目前ios动画主要分两种,一种是基于UIView的动画,一种是Core Animation,今天先讨论UIView动画。
实际上,在UIKIt框架内的UIView动画还是基于Core Animation封装的,只不过面对开发者来说,更简单了一些。UIView的动画是改变UIView以下属性产生的:
1.@porperty frame 在父视图中位置和大小
2.@property bounds 基于本地坐标的位置和大小
3.@property center 视图的中心点
4.@property transform 视图的变化矩阵
5.@property alpha 视图的透明度
6.@property backgroundColor 背景色
UIView动画函数有以下几个:
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations;
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void(^)(void))animations completion:(void(^)(BOOLfinished))completion;
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
这些函数使用起来都比较简单,上代码
[UIViewanimateWithDuration:2animations:^(void){
[self.tempImgViewsetFrame:CGRectMake(frame.origin.x+200, frame.origin.y-150, frame.size.width, frame.size.height)];
}completion:nil];//改变图片的位置,产生移动的动画效果
CGRectnewRect=CGRectMake(0, 0, 120, 200);
[UIViewanimateWithDuration:2animations:^(void){
[self.tempImgViewsetBounds:newRect];
}completion:^(BOOLflag){
[UIViewanimateWithDuration:2animations:^(void){
[self.tempImgViewsetBounds:bounds];
}completion:nil];
}];//改变图片的bounds属性,使图片先变大在变回原样
self.tempImgView.image=[UIImageimageNamed:@"wukong"];
[UIViewanimateWithDuration:2animations:^(void){
self.tempImgView.transform=CGAffineTransformMakeScale(0.6, 0.6);
}completion:^(BOOLflag){
self.tempImgView.image=[UIImageimageNamed:@"bee"];
}
];//使图片变小,然后改变图片内容
self.tempImgView.image=[UIImageimageNamed:@"wukong"];
[UIViewanimateWithDuration:2animations:^(void){
self.tempImgView.alpha=0.1 ;
}completion:nil];//使图片变透明