一. UIViewPropertyAnimator的介紹
UIViewPropertyAnimator 可以稱為屬性動畫器溃睹,是iOS10中新增的一個執(zhí)行View動畫的類口予,它主要具有一下的特點:
1. 可中斷性
2. 可擦除
3. 可反轉(zhuǎn)性
4. 豐富的動畫時間控制功能
以前我們執(zhí)行view的動畫,基本上都是不可以中斷的没卸,這次出了UIViewPropertyAnimator后枯冈,我們可以任意中斷動畫和反轉(zhuǎn)繼續(xù)動畫,提高了動畫的可操作性办悟。
UIViewPropertyAnimator是遵循UIViewImplicitlyAnimating協(xié)議
UIViewImplicitlyAnimating遵循UIViewAnimating協(xié)議
二. 代碼實現(xiàn)
1.我們首先添加要進行一些動畫操作的view:
self.testView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.testView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.testView];
2.接下來我們可以初始化屬性動畫器了:
@property (strong, nonatomic) UIViewPropertyAnimator *viewProperty;
//初始化屬性動畫器
self.viewProperty = [[UIViewPropertyAnimator alloc] initWithDuration:4.f curve:UIViewAnimationCurveLinear animations:^{
self.testView.frame = CGRectMake(200, 200, 200, 200);
}];
在初始化屬性動畫器中尘奏,我們讓testview由初始位置CGRectMake(100, 100, 100, 100)移動到CGRectMake(200, 200, 200, 200),當然這樣只是初始化病蛉,testview并不會真正的移動炫加,如果想讓view移動起來瑰煎,我們需要:
[self.viewProperty startAnimation];
這段代碼就可以了,同樣俗孝,我們可以讓view做一些其他的動畫:
暫停view動畫:
[self.viewProperty pauseAnimation];
停止view動畫:
[self.viewProperty stopAnimation:true];
其中stopAnimation:后跟了一個BOOL值酒甸,它的含義是 是否在動畫執(zhí)行結(jié)束后才停止動畫。
繼續(xù)動畫:
UISpringTimingParameters *param = [[UISpringTimingParameters alloc] initWithDampingRatio:0.1];
[self.viewProperty continueAnimationWithTimingParameters:param durationFactor:1];
其中continueAnimationWithTimingParameters后面需要一個UITimingCurveProvider的參數(shù)赋铝,這個參數(shù)的含義就是接下來的動畫插勤,我們想要以什么樣的形式執(zhí)行下去,這里我選擇了spring的形式革骨,也就是彈簧的形式农尖,durationFactor后面的參數(shù)意思是繼續(xù)執(zhí)行下去的動畫效果在整個動畫中的時間,我選擇了1s良哲。