老规矩先放一张美图吧。

iOS中使用简单动画循环其中有两种方式
1.使用animation中自带的AnimationRepeatCount ,可以通过设置这个值,使动画能循环运行多少次,如果要设置为一直无限循环,可以设置为MAXFLOAT,参考代码如下:
[UIView animateWithDuration:1.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
[UIView setAnimationRepeatCount:MAXFLOAT];
_bottomConstraint.constant = 300;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
_bottomConstraint.constant = 0;
[self.view layoutIfNeeded];
}];
需要注意的地方为:
1.1动画代码如果想要在页面开始的时候就运行一定不要放到viewDidLoad中,一定要放到viewDidAppear或者viewWillAppear中,否则只显示动画的最终完成的效果
1.2动画代码中 [UIView setAnimationRepeatCount:MAXFLOAT]这一段代码一定要放到所有效果代码之前,否则动画无法循环
2.可以采用定时器的方法做动画循环参考代码如下:
NSTimer *splashTimer = nil;
splashTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(anmimationView) userInfo:nil repeats:YES];
然后在放上动画代码:
_bottomConstraint.constant = 300;
[self.view layoutIfNeeded];
[UIView animateWithDuration:1.0f animations:^{
UIView.animationRepeatCount = 9999;
_bottomConstraint.constant =0;
[self.view layoutIfNeeded];
}];
这样就能实现动画的循环了。
在iOS开发中经常会遇到各种各样的坑,慢慢积累,大师之路就在脚下。
网友评论