1、UIView動(dòng)畫介紹
1.簡(jiǎn)單說明
UIKit直接將動(dòng)畫集成到UIView類中,當(dāng)內(nèi)部的一些屬性發(fā)生改變時(shí),UIView將為這些改變提供動(dòng)畫支持
執(zhí)行動(dòng)畫所需要的工作由UIView類自動(dòng)完成,但仍要在希望執(zhí)行動(dòng)畫時(shí)通知視圖,為此需要將改變屬性的代碼放在[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之間
常見方法解析:
+(void)setAnimationDelegate:(id)delegate 設(shè)置動(dòng)畫代理對(duì)象,當(dāng)動(dòng)畫開始或者結(jié)束時(shí)會(huì)發(fā)消息給代理對(duì)象
+(void)setAnimationWillStartSelector:(SEL)selector 當(dāng)動(dòng)畫即將開始時(shí),執(zhí)行delegate對(duì)象的selector,并且把beginAnimations:context:中傳入的參數(shù)傳進(jìn)selector
+(void)setAnimationDidStopSelector:(SEL)selector 當(dāng)動(dòng)畫結(jié)束時(shí),執(zhí)行delegate對(duì)象的selector,并且把beginAnimations:context:中傳入的參數(shù)傳進(jìn)selector
+(void)setAnimationDuration:(NSTimeInterval)duration 動(dòng)畫的持續(xù)時(shí)間,秒為單位
+(void)setAnimationDelay:(NSTimeInterval)delay 動(dòng)畫延遲delay秒后再開始
+(void)setAnimationStartDate:(NSDate *)startDate 動(dòng)畫的開始時(shí)間,默認(rèn)為now
+(void)setAnimationCurve:(UIViewAnimationCurve)curve 動(dòng)畫的節(jié)奏控制
+(void)setAnimationRepeatCount:(float)repeatCount 動(dòng)畫的重復(fù)次數(shù)
+(void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses 如果設(shè)置為YES,代表動(dòng)畫每次重復(fù)執(zhí)行的效果會(huì)跟上一次相反
+(void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache 設(shè)置視圖view的過渡效果, transition指定過渡類型, cache設(shè)置YES代表使用視圖緩存,性能較好
2、UIView動(dòng)畫代碼
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *myView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"動(dòng)畫開始執(zhí)行之前myView的中心點(diǎn):%@",NSStringFromCGPoint(self.myView.center));
NSLog(@"---------------------------");
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[UIView animateWithDuration:2.0 animations:^{
self.myView.center = CGPointMake(50, 300);
}];
NSLog(@"動(dòng)畫開始執(zhí)行之后myView的中心點(diǎn):%@",NSStringFromCGPoint(self.myView.center));
}
@end
3、動(dòng)畫執(zhí)行效果


4、UIView封裝的動(dòng)畫與CALayer動(dòng)畫的對(duì)比
使用UIView和CALayer都能實(shí)現(xiàn)動(dòng)畫效果,但是在真實(shí)的開發(fā)中,一般還是主要使用UIView封裝的動(dòng)畫,而很少使用CALayer的動(dòng)畫。
CALayer核心動(dòng)畫與UIView動(dòng)畫的區(qū)別:
UIView封裝的動(dòng)畫執(zhí)行完畢之后不會(huì)反彈。即如果是通過CALayer核心動(dòng)畫改變layer的位置狀態(tài),表面上看雖然已經(jīng)改變了,但是實(shí)際上它的位置是沒有改變的。
5、block動(dòng)畫
- +(void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
參數(shù)解析:
duration:動(dòng)畫的持續(xù)時(shí)間
delay:動(dòng)畫延遲delay秒后開始
options:動(dòng)畫的節(jié)奏控制
animations:將改變視圖屬性的代碼放在這個(gè)block中
completion:動(dòng)畫結(jié)束后,會(huì)自動(dòng)調(diào)用這個(gè)block
轉(zhuǎn)場(chǎng)動(dòng)畫
- +(void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
參數(shù)解析:
duration:動(dòng)畫的持續(xù)時(shí)間
view:需要進(jìn)行轉(zhuǎn)場(chǎng)動(dòng)畫的視圖
options:轉(zhuǎn)場(chǎng)動(dòng)畫的類型
animations:將改變視圖屬性的代碼放在這個(gè)block中
completion:動(dòng)畫結(jié)束后,會(huì)自動(dòng)調(diào)用這個(gè)block
- +(void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
方法調(diào)用完畢后,相當(dāng)于執(zhí)行了下面兩句代碼:
// 添加toView到父視圖
[fromView.superview addSubview:toView];
// 把fromView從父視圖中移除
[fromView.superview removeFromSuperview];
參數(shù)解析:
duration:動(dòng)畫的持續(xù)時(shí)間
options:轉(zhuǎn)場(chǎng)動(dòng)畫的類型
animations:將改變視圖屬性的代碼放在這個(gè)block中
completion:動(dòng)畫結(jié)束后,會(huì)自動(dòng)調(diào)用這個(gè)block
6、block動(dòng)畫示例
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *myView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"動(dòng)畫開始執(zhí)行之前myView的中心點(diǎn):%@",NSStringFromCGPoint(self.myView.center));
NSLog(@"---------------------------");
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// block動(dòng)畫
[UIView transitionWithView:self.myView duration:2.0 options:UIViewAnimationOptionAutoreverse animations:^{
self.myView.center = CGPointMake(50, 300);
} completion:^(BOOL finished) {
NSLog(@"動(dòng)畫開始執(zhí)行之后myView的中心點(diǎn):%@",NSStringFromCGPoint(self.myView.center));
}];
}
@end
這個(gè)轉(zhuǎn)場(chǎng)動(dòng)畫的效果是由+(void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion這個(gè)方法的options參數(shù)決定的。