目前ios動畫主要分兩種,一種是基于UIView的動畫,一種是Core Animation,今天先討論UIView動畫茫负。
實際上,在UIKIt框架內的UIView動畫還是基于Core Animation封裝的乎赴,只不過面對開發(fā)者來說忍法,更簡單了一些。UIView的動畫是改變UIView以下屬性產(chǎn)生的:
1.@porperty frame ? 在父視圖中位置和大小
2.@property bounds 基于本地坐標的位置和大小
3.@property center ?視圖的中心點
4.@property transform ?視圖的變化矩陣
5.@property alpha ?視圖的透明度
6.@property backgroundColor ?背景色
UIView動畫函數(shù)有以下幾個:
+ (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;
這些函數(shù)使用起來都比較簡單榕吼,上代碼
[UIViewanimateWithDuration:2animations:^(void){
[self.tempImgViewsetFrame:CGRectMake(frame.origin.x+200, frame.origin.y-150, frame.size.width, frame.size.height)];
}completion:nil];//改變圖片的位置饿序,產(chǎn)生移動的動畫效果
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];//使圖片變透明