[TOC]
UIView动画
UIView本身也含有动画的API:分为2种:属性动画、翻转动画
- 属性动画:可以设置frame、bounds、center、alpha、transform、backgroundColor、contextStretch
- 翻转动画: transition
iOS4.0之前的方式
//iOS4.0之前的方式:
//需要将改变属性的代码放在[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之间
常见方法解析:
+ (void)setAnimationDelegate:(id)delegate 设置动画代理对象,当动画开始或者结束时会发消息给代理对象
+ (void)setAnimationWillStartSelector:(SEL)selector 当动画即将开始时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector
+ (void)setAnimationDidStopSelector:(SEL)selector 当动画结束时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector
+ (void)setAnimationDuration:(NSTimeInterval)duration 动画的持续时间,秒为单位
+ (void)setAnimationDelay:(NSTimeInterval)delay 动画延迟delay秒后再开始
+ (void)setAnimationStartDate:(NSDate *)startDate 动画的开始时间,默认为now
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve 动画的节奏控制
+ (void)setAnimationRepeatCount:(float)repeatCount 动画的重复次数
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses 如果设置为YES,代表动画每次重复执行的效果会跟上一次相反
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache 设置视图view的过渡效果, transition指定过渡类型, cache设置YES代表使用视图缓存,性能较好
//e.g.
UIImageView *imageView=(UIImageView *)[self.window viewWithTag:100];
[UIView beginAnimations:@"animation" context:nil]; //开始设置
[UIView setAnimationDuration:5];
[UIView setAnimationDelay:0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationEnd)];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
imageView.transform=CGAffineTransformRotate(imageView.transform, M_PI);
[UIView commitAnimations]; //开始动画
iOS4.0之后采用block方法
//1. 属性动画
UIImageView *imageView=(UIImageView*)[self.window viewWithTag:100];
[UIView animateWithDuration:2 animations:^{
CGRect rect=imageView.frame;
rect.origin.x+=100;
rect.origin.y+=100;
rect.size.height=50;
rect.size.width=50;
imageView.frame=rect;
}];
//可以设置动画结束后执行的操作
[UIView animateWithDuration:2 animations:^{
} completion:^(BOOL finished) {
}];
//可以设置动画的持续时间、延迟时间、动画播放的开始结束速度、动画结束后执行的操作
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
} completion:^(BOOL finished) {
}];
//2. 翻转动画
UIView *view1=[self.window viewWithTag:10];
UIView *view2=[self.window viewWithTag:20];
//设置视图翻转,将view2翻转到view1位置
[UIView transitionFromView:view2 toView:view1 duration:2 options:UIViewAnimationOptionTransitionCurlDown completion:^(BOOL finished) {
NSLog(@"transition finished");
}];
//将某一个视图翻转
[UIView transitionWithView:nil duration:2
options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
} completion:^(BOOL finished) {
}];
//位移变换
-(void)positionAnimation{
self.imgv.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:1.0f animations:^{
self.imgv.transform = CGAffineTransformMakeTranslation(100, 100);
}];
}
//缩放变换
-(void)scaleAnimation{
self.imgv.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:1.0f animations:^{
self.imgv.transform = CGAffineTransformMakeScale(2, 2);
}];
}
//旋转变换
-(void)rotateAnimation{
self.imgv.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:1.0f animations:^{
self.imgv.transform = CGAffineTransformMakeRotation(M_PI);
}];
}
//组合变换
-(void)combinationAnimation{
//仿射变换的组合使用
self.imgv.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:1.0f animations:^{
CGAffineTransform transform1 = CGAffineTransformMakeRotation(M_PI);
CGAffineTransform transform2 = CGAffineTransformScale(transform1, 0.5, 0.5);
self.imgv.transform = CGAffineTransformTranslate(transform2, 100, 100);
}];
}
//矩阵的反转
-(void)invertAnimation{
self.imgv.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:1.0f animations:^{
//矩阵反转
self.imgv.transform = CGAffineTransformInvert(CGAffineTransformMakeScale(2, 2));
}];
}