一 . UIview動畫本質是對Core Animation的封裝, 優點就是簡單方便使用,缺點就是不靈活...(個人感覺)
簡單使用:
1.開始動畫:
[UIView beginAnimations:@"testAnimate" context:@"info"];
//第一個參數是動畫標識 第二個參數是要傳遞的信息 一般為空,這個信息和標識是在代理中可以獲取到.
2.設置代理
+ (void)setAnimationWillStartSelector:(nullable SEL)selector;
// default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context
+ (void)setAnimationDidStopSelector:(nullable SEL)selector;
// default = NULL.
這是官方API給出的代理方法,
我們需要設置代理:
[UIView setAnimationDelegate:self];
在動畫開始和結束就會走上面2個協議方法.
其他方法:
+(void)setAnimationDuration:(NSTimeInterval)duration; //設置動畫執行的時間間隔.
+(void)setAnimationDelay:(NSTimeInterval)delay; //設置動畫多久后執行
+(void)setAnimationStartDate:(NSDate *)startDate; //設置動畫開始時間
+(void)setAnimationCurve:(UIViewAnimationCurve)curve; //曲線運動 a=Δv/Δt 物理中的加速度 使其做非勻速運動
UIViewAnimationCurve參數:
UIViewAnimationCurveEaseInOut, 慢進慢出
UIViewAnimationCurveEaseIn, 慢進
UIViewAnimationCurveEaseOut, 慢出
UIViewAnimationCurveLinear, 勻速+(void)setAnimationRepeatCount:(float)repeatCount; //設置動畫重復執行次數
+(void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;//動畫是否逆執行 (反向動畫)
+(void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState; //設置為yes 動畫從當前狀態執行 否則是動畫結束后執行
-
+(void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache; //設置動畫翻轉效果
UIViewAnimationTransition參數:
//UIViewAnimationTransitionNone, 無動畫
//UIViewAnimationTransitionFlipFromLeft, 從左到右翻頁
//UIViewAnimationTransitionFlipFromRight, 從右到左翻頁
//UIViewAnimationTransitionCurlUp, 上到下
//UIViewAnimationTransitionCurlDown, 下到上 +(void)setAnimationsEnabled:(BOOL)enabled; //是否允許動畫
以上就是UIView(UIViewANimation)常用的方法, 這寫方法大多都是IOS4出的,不帶Block的方式 ,使用就是在開始動畫和結束動畫之間寫動畫.
完整代碼:
// self.animateView 是我自己創建的動畫視圖
- (void)setAnimateNormal{
//基礎方式(比較古老了iOS4 以后用block替代了)
//開始動畫
//參數 開始動畫的標識 和 信息 (可以在動畫的代理方法得到)
[UIView beginAnimations:@"testAnimate" context:@"info"];
//代理
[UIView setAnimationDelegate:self];
// //設置代理后 實現的方法 開始和結束的方法
[UIView setAnimationDidStopSelector:@selector(AnimateStop:Context:)];
[UIView setAnimationWillStartSelector:@selector(AnimateStart:Context:)];
//延時操作 default = 0.0
[UIView setAnimationDelay:2.0f];
//動畫時間間隔 default = 0.2f
[UIView setAnimationDuration:3.0f];
// NSString *time = @"2018-01-16 14:48:50";
// NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// NSDate *date = [formatter dateFromString:time];
//默認開始時間是現在
// [UIView setAnimationStartDate:[NSDate date]];
//曲線動畫 a=Δv/Δt 物理中的加速度 使其做非勻速運動
//UIViewAnimationCurveEaseInOut, 慢進慢出
//UIViewAnimationCurveEaseIn, 慢進
//UIViewAnimationCurveEaseOut, 慢出
//UIViewAnimationCurveLinear, 勻速
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
// 0 , 1 都是一次 long_max = 無窮大
[UIView setAnimationRepeatCount:LONG_MAX];
//是否執行相反的動畫 default = NO
[UIView setAnimationRepeatAutoreverses:YES];
//設置為YES動畫從當前狀態進行新的動畫, NO從結束狀態進行新動畫
[UIView setAnimationBeginsFromCurrentState:YES];
//UIViewAnimationTransitionNone, 無動畫
//UIViewAnimationTransitionFlipFromLeft, 從左到右翻頁
//UIViewAnimationTransitionFlipFromRight, 從右到左翻頁
//UIViewAnimationTransitionCurlUp, 上到下
//UIViewAnimationTransitionCurlDown, 下到上
//[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.animateView cache:YES];
//是否允許動畫
[UIView setAnimationsEnabled:YES];
BOOL enable = [UIView areAnimationsEnabled];
NSLog(@"%d",enable);
//繼承動畫間隔時間 iOS9新出的
int tim = [UIView inheritedAnimationDuration];
NSLog(@"%d",tim);
#if UIKIT_DEFINE_AS_PROPERTIES
//是屬性的時候 執行
#else
//類方法執行
#endif
//某些地方不需要執行動畫可以在這個方法里寫 iOS 7 后出的方法
[UIView performWithoutAnimation:^{
self.animateView.frame = CGRectMake(200, 400, 100, 100);
}];
self.animateView.frame = CGRectMake(100, 400, 100, 100);
self.animateView.alpha = 0;
//結束動畫
[UIView commitAnimations];
}
//代理
- (void)AnimateStop:(id)obj Context:(id)text{
NSLog(@"結束:%@,--- %@",obj, text);
}
- (void)AnimateStart:(id)obj Context:(id)text{
NSLog(@"開始:%@--%@",obj,text);
}
//注意: 動畫要在結束動畫 [UIView commitAnimations] 前寫
二. 上面是直接使用的方式, 不過蘋果給這些動畫進行了封裝,就是上面提到過的UIView(UIViewAnimationWithBlocks) Block方式, API里給出我們7個方法.
+(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
//這個是最簡單的方法, 參數duration是執行時間, animations回調是我們在里寫的動畫, completion回調是動畫結束后要進行操作的地方.+(void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
//delay: 延時執行時間
options:
UIViewAnimationOptionLayoutSubviews:子控件隨父控件一起動畫
UIViewAnimationOptionAllowUserInteraction: 動畫時允許用戶觸摸操作 允許交互
UIViewAnimationOptionBeginFromCurrentState:從當前狀態開始動畫
UIViewAnimationOptionRepeat:一直重復動畫
UIViewAnimationOptionAutoreverse :動畫結束后反向執行動畫
UIViewAnimationOptionOverrideInheritedDuration:忽略嵌套動畫時間設置
UIViewAnimationOptionOverrideInheritedCurve:忽略嵌套動畫速度設置
UIViewAnimationOptionAllowAnimatedContent:動畫執行重繪視圖
UIViewAnimationOptionShowHideTransitionViews:隱藏舊視圖 顯示新視圖
UIViewAnimationOptionOverrideInheritedOptions :不繼承父動畫設置動畫類型
---------------------------------------------------------------------------
//同UIView的曲線運動
UIViewAnimationOptionCurveEaseInOut
UIViewAnimationOptionCurveEaseIn
UIViewAnimationOptionCurveEaseOut
UIViewAnimationOptionCurveLinear
---------------------------------------------------------------------------
//同上方轉場動畫
UIViewAnimationOptionTransitionNone
UIViewAnimationOptionTransitionFlipFromLeft
UIViewAnimationOptionTransitionFlipFromRight
UIViewAnimationOptionTransitionCurlUp
UIViewAnimationOptionTransitionCurlDown
UIViewAnimationOptionTransitionCrossDissolve
UIViewAnimationOptionTransitionFlipFromTop
UIViewAnimationOptionTransitionFlipFromBottom
+(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0);//最基礎的方法
+(void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);
//duration: 時間間隔, delay: 延時執行 Damping: 彈簧效果 0.f-1.f
//initial: 初始加速度
//大多數iOS系統動畫都是用這種方式 彈性效果+(void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
//轉場動畫 duration: 時間間隔 options: 動畫類型 animations: 需要做的動畫+(void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
//IOS 4出的方法
//FromView: 從父視圖移除, ToView: 添加到父視圖, duration: 時間間隔, options: 動畫效果 (基本不怎么用 有屬性可以替代)
這些應該就是可能 emmm... 常用的
詳細代碼:
- (void)setAnimateWithBlock{
//block動畫 參數: 時間間隔 和 動畫回調
#if Controller_Not_Allow_Run
[UIView animateWithDuration:3.f animations:^{
self.animateView.frame = CGRectMake(100, 400, 100, 100);
self.animateView.alpha = 0;
}];
#endif
//比上一個方法多一個結束回調
[UIView animateWithDuration:3.f animations:^{
self.animateView.frame = CGRectMake(100, 400, 100, 100);
self.animateView.alpha = 0;
} completion:^(BOOL finished) {
self.animateView.alpha = 1;
}];
// UIViewAnimationOptionLayoutSubviews 子控件隨父控件一起動畫
// UIViewAnimationOptionAllowUserInteraction 動畫時允許用戶觸摸操作 允許交互
// UIViewAnimationOptionBeginFromCurrentState 從當前狀態開始動畫
// UIViewAnimationOptionRepeat 一直重復動畫
// UIViewAnimationOptionAutoreverse 動畫結束后反向執行動畫
// UIViewAnimationOptionOverrideInheritedDuration
// UIViewAnimationOptionOverrideInheritedCurve
// UIViewAnimationOptionAllowAnimatedContent
// UIViewAnimationOptionShowHideTransitionViews
// UIViewAnimationOptionOverrideInheritedOptions
// UIViewAnimationOptionCurveEaseInOut
// UIViewAnimationOptionCurveEaseIn
// UIViewAnimationOptionCurveEaseOut
// UIViewAnimationOptionCurveLinear
// UIViewAnimationOptionTransitionNone
// UIViewAnimationOptionTransitionFlipFromLeft
// UIViewAnimationOptionTransitionFlipFromRight
// UIViewAnimationOptionTransitionCurlUp
// UIViewAnimationOptionTransitionCurlDown
// UIViewAnimationOptionTransitionCrossDissolve
// UIViewAnimationOptionTransitionFlipFromTop
// UIViewAnimationOptionTransitionFlipFromBottom
// UIViewAnimationOptionPreferredFramesPerSecondDefault
// UIViewAnimationOptionPreferredFramesPerSecond60
// UIViewAnimationOptionPreferredFramesPerSecond30
/******************************** block 3 **********************************/
#if Controller_Not_Allow_Run
//執行動畫的block 比較簡單的方式
[UIView animateWithDuration:3.f delay:2.f options:UIViewAnimationOptionAutoreverse animations:^{
self.animateView.frame = CGRectMake(100, 400, 100, 100);
} completion:^(BOOL finished) {
}];
#endif
/******************************** block 4 **********************************/
//duration: 時間間隔, delay: 延時執行 Damping: 彈簧效果 0.f-1.f
// initial: 初始加速度
//大多數iOS系統動畫都是用這種方式 彈性效果
#if Controller_Not_Allow_Run
[UIView animateWithDuration:0.5f delay:1.f usingSpringWithDamping:0.2 initialSpringVelocity:0.5 options:UIViewAnimationOptionLayoutSubviews animations:^{
self.animateView.frame = CGRectMake(100, 400, 100, 100);
} completion:^(BOOL finished) {
NSLog(@"結束");
}];
#endif
/******************************** block 5 **********************************/
#if Controller_Not_Allow_Run
//轉場動畫 duration: 時間間隔 options: 動畫類型 animations: 需要做的動畫
[UIView transitionWithView:self.animateView duration:0.5 options:0 animations:^{
self.animateView.center = CGPointMake(250, 250);
} completion:^(BOOL finished) {
}];
#endif
/******************************** block 6 **********************************/
//IOS 4出的方法
//FromView: 從父視圖移除, ToView: 添加到父視圖, duration: 時間間隔, options: 動畫效果
#if Controller_Not_Allow_Run
UIView *removeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
removeView.backgroundColor = [UIColor grayColor];
[self.view addSubview:removeView];
[UIView transitionFromView:removeView toView:self.animateView duration:1.f options:0 completion:^(BOOL finished) {
NSLog(@"結束");
}];
#endif
// ***** 宏自行刪除 *****
這些動畫都是一次動畫 只能進行一些比較簡單的動畫, 位移 ,放大 ,縮小, 透明度,旋轉等等.
下篇我會寫CAAnimation
本篇代碼地址: Demo