Core Animation 是iOS中一個比較重要的框架,許多炫酷動畫和效果都可以用Core Animation 來實現(xiàn),下面我們先簡單的認(rèn)識了解下Core Animation.
認(rèn)識 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ù)查看 我們能看到更多的類,多層次繼承關(guān)系
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];
//默認(rèn)是NO? YES 表示重復(fù)循環(huán)
animation.autoreverses=YES;
animation.duration=1;
animation.repeatCount=10;//重復(fù)次數(shù)
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 的轉(zhuǎn)場動畫
之前我們就知道CATransition繼承于CAAniamtion ,CATransition用于做轉(zhuǎn)場動畫恢共,能夠為層提供移出屏幕和移入屏幕的動畫效果砂心。
@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的值解讀對應(yīng)常量
fade ? ? ? 淡入淡出 ? ?kCATransitionFade
push ? ? ? ? 推擠 ? ? ? ? kCATransitionPush
reveal ? ? ? 揭開 ? ? ? ? kCATransitionReveal
moveIn ? ?覆蓋 ? ? ? ? ?kCATransitionMoveIn
cube ? ? ? 立方體 ? ? ? ? ? 私有API
suckEffect 吮吸 ? ? ? ? ? 私有API
oglFlip ? ? ? ? 翻轉(zhuǎn) ? ? ? ? ? 私有API
rippleEffect ?波紋 ? ? ? ? 私有API
pageCurl ? ? 反翻頁 ? ? ? ? ?私有API
cameraIrisHollowOpen ?開鏡頭 ? ?私有API
cameraIrisHollowClose ? 關(guān)鏡頭 ? 私有API
過渡方向參數(shù)
kCATransitionFromRight ? ? ?從右轉(zhuǎn)場
kCATransitionFromLeft ? ? ? ? 從左轉(zhuǎn)場
kCATransitionFromBottom ? ?從下轉(zhuǎn)場
kCATransitionFromTop ? ? ? ? ?從上轉(zhuǎn)場
上代碼 ?imageView 放了一張女神王祖賢的照片
//轉(zhuǎn)場動畫
-(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];
}