Pulsating UIView

Pulsating UIView

Original Address: http://tim-specht.de/?p=695

I needed to create a pulsating UIImageView (this will work for any subclass including itself of UIView without any changes) and solved that task using chained UIViewAnimation so I wanted to share that bunch of code with you.

//this method is used to setup all the stuff any initially starting the chain as mentioned

-(void)addLoadingImage{

UIImageView* pulseView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]];

pulseView.tag = MY_PULSE_TAG; //needed to keep track of the view afterwards, change this to whatever you want (use #define for better style, this is just a quick writedown!)

[self.view addSubview:pulseView];

[self pulseBig];

}

//animating the pulsing to big by changing the frame of the view which should be pulsing

-(void)pulseBig{

//needed to stop animation if view got removed, otherwhise we will get EXC_BAD_ACCESS

if(![self.view viewWithTag:MY_PULSE_TAG])

return;

[UIView beginAnimations:@"pulsing" context:nil];

[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

[UIView setAnimationDuration:1];

[UIView setAnimationDelegate:self];

[UIView setAnimationDidStopSelector:@selector(pulseSmall)];

[[self.view viewWithTag:MY_PULSE_TAG] setFrame:myBigFrame];

[UIView commitAnimations];

 

}

//pulsing the view small!

-(void)pulseSmall{

//needed to stop animation if view got removed, otherwhise we will get EXC_BAD_ACCESS

if(![self.view viewWithTag:MY_PULSE_TAG])

return;

[UIView beginAnimations:@"pulsing" context:nil];

[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

[UIView setAnimationDuration:1];

[UIView setAnimationDelegate:self];

[UIView setAnimationDidStopSelector:@selector(pulseSmall)];

[[self.view viewWithTag:MY_PULSE_TAG] setFrame:mySmallFrame];

[UIView commitAnimations];

}



Just fill in your two frames and the tag and you are ready to go. Please be aware that you need to set the origin of the bigger frame half of the changed size to be sure the center remains the same.

猜你喜欢

转载自zl4393753.iteye.com/blog/1759344