先创建一个UILabel
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,0,50,50)];
label.backgroundColor = [UIColor blueColor];
[self.window addSubview:label];
[UIView beginAnimations:nil context:nil];
动画开始
[UIView setAnimationDuration:4];
动画持续时间
[UIView setAnimationRepeatCount:1];
动画重复次数
label.frame = CGRectMake(100, 100, 50, 50);
动画结束是label的状态
[UIView commitAnimations];
提交动画
上述动画只进行了最基本的动画设置,只改变了label的位置,运行时label将在4S的时间内从(0,0)移动到(100,100)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:4];
[UIView setAnimationRepeatCount:1];
label.frame = CGRectMake(100, 100, 50, 50);
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(whenStop)];
添加动画结束之后的时间触发,即四秒动画执行完之后会执行whenStop方法
[UIView commitAnimations];
这里要提把这句拿出来看
[UIView beginAnimations:nil context:nil];
beginAnimation:动画块儿标示符,即给本次动画命名可以传递给动画结束方法参数为任意NSString
context:用于传递参数,传递至动画结束方法
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
指定动画结束方法:animationID:动画标示符,对应beginAnimation的参数,用于区分哪个动画块儿调用此方法;finished:用于验证动画是否执行成功;context:用于接收动画context传过来的参数
若再次创建结束方法,需保持格式一致,如
-(void)aniStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
其他动画效果
[UIView setAnimationRepeatAutoreverses:YES];
是否进行反项动画
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:window cache:YES];
页面切换效果,写在页面切换之后可进行页面切换效果设置,获取window方式
UIWindow * window = [[[UIApplication sharedApplication]delegate]window];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
动画切入和结束时是否减速的效果
帧动画
imageView.animationDuration = 2;
imageView.animationRepeatCount = 1;
imageView.animationImages = array;
[imageView startAnimating];
帧动画的主体是一个ImageView,设置其持续时间和重复次数,array 是一个数组,用于存放帧动画播放的图片,然后提交动画