`
zl4393753
  • 浏览: 340145 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

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.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics