簡單動畫 (二) 之Core Animation

Core Animation 是iOS中一個比較重要的框架,許多炫酷動畫和效果都可以用Core Animation 來實現,下面我們先簡單的認識了解下Core Animation.

認識 Core Animation

在QuartzCore這個framwork 中提供了一下這些類


在Core Animation中提供了一個基本類 CAAnimation 通過查看文檔我們可以看到一些方法和屬性

@interface CAAnimation : NSObject

{@private? void *_attr;?

?uint32_t _flags;

}

/* Creates a new animation object. */

+ (instancetype)animation;

/* Animations implement the same property model as defined by CALayer. * See CALayer.h for more details. */

+ (nullable id)defaultValueForKey:(NSString *)key;

- (BOOL)shouldArchiveValueForKey:(NSString *)key;

/* A timing function defining the pacing of the animation. Defaults to * nil indicating linear pacing. */

@property(nullable, strong) CAMediaTimingFunction *timingFunction;

/* The delegate of the animation. This object is retained for the * lifetime of the animation object. Defaults to nil. See below for the * supported delegate methods. */

@property(nullable, strong) id delegate;

/* When true, the animation is removed from the render tree once its

* active duration has passed. Defaults to YES. */

@property(getter=isRemovedOnCompletion) BOOL removedOnCompletion;

@end

這個類是最基本的抽象類 ,主要提供了創(chuàng)建類方法 ,動畫執(zhí)行方法 以及是否在執(zhí)行完成后移除等.

我們常用到的動畫主要涉及到以下幾個類

CABasicAnimation、CAKeyframeAnimation、CATransition、CAAnimationGroup 、CASpringAnimation

在文檔往下繼續(xù)查看 我們能看到更多的類,多層次繼承關系

CAPropertyAnimation : CAAnimation

CABasicAnimation : CAPropertyAnimation

CAKeyframeAnimation : CAPropertyAnimation

CASpringAnimation : CABasicAnimation

CAAnimationGroup : CAAnimation

CATransition : CAAnimation


1 ? ?CABasicAnimation 的使用

? 改變透明度

- (IBAction)baseClick:(id)sender {

CABasicAnimation* animation=[CABasicAnimation animationWithKeyPath:@"opacity"];

animation.fromValue=[NSNumber numberWithFloat:1.0];

animation.toValue=[NSNumber numberWithFloat:0.00];

//默認是NO? YES 表示重復循環(huán)

animation.autoreverses=YES;

animation.duration=1;

animation.repeatCount=10;//重復次數

animation.removedOnCompletion=NO;//執(zhí)行完成后是否移除

animation.fillMode=kCAFillModeForwards;//效果可選擇

/** Timing function names.

CA_EXTERN NSString * const kCAMediaTimingFunctionLinear

CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);

CA_EXTERN NSString * const kCAMediaTimingFunctionEaseIn

CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);

CA_EXTERN NSString * const kCAMediaTimingFunctionEaseOut

CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);

CA_EXTERN NSString * const kCAMediaTimingFunctionEaseInEaseOut

CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);

CA_EXTERN NSString * const kCAMediaTimingFunctionDefault

CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0);

NS_ASSUME_NONNULL_END

**/

animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

[self.baseBtn.layer addAnimation:animation forKey:@"alpha"];

}

效果




2 ? CATransition 的轉場動畫

之前我們就知道CATransition繼承于CAAniamtion ,CATransition用于做轉場動畫,能夠為層提供移出屏幕和移入屏幕的動畫效果。


@interface CATransition : CAAnimation

@property(copy) NSString *type;? //動畫過渡類型

@property(nullable, copy) NSString *subtype; //動畫過渡方向

@property float startProgress;

@property float endProgress;

@property(nullable, strong) id filter;

@end

type的值解讀對應常量

fade ? ? ? 淡入淡出 ? ?kCATransitionFade

push ? ? ? ? 推擠 ? ? ? ? kCATransitionPush

reveal ? ? ? 揭開 ? ? ? ? kCATransitionReveal

moveIn ? ?覆蓋 ? ? ? ? ?kCATransitionMoveIn

cube ? ? ? 立方體 ? ? ? ? ? 私有API

suckEffect 吮吸 ? ? ? ? ? 私有API

oglFlip ? ? ? ? 翻轉 ? ? ? ? ? 私有API

rippleEffect ?波紋 ? ? ? ? 私有API

pageCurl ? ? 反翻頁 ? ? ? ? ?私有API

cameraIrisHollowOpen ?開鏡頭 ? ?私有API

cameraIrisHollowClose ? 關鏡頭 ? 私有API

過渡方向參數

kCATransitionFromRight ? ? ?從右轉場

kCATransitionFromLeft ? ? ? ? 從左轉場

kCATransitionFromBottom ? ?從下轉場

kCATransitionFromTop ? ? ? ? ?從上轉場

上代碼 ?imageView 放了一張女神王祖賢的照片

//轉場動畫

-(void)maketransAnimation

{

CATransition* trans=[CATransition animation];

trans.repeatCount=1000;

// trans.type= kCATransitionPush;

trans.type=@"oglFlip";

trans.subtype=kCATransitionFromRight;

trans.duration=1.2;

trans.autoreverses=YES;

[self.imageView.layer addAnimation:trans forKey:nil];

}


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容