1: 什么是CABasicAnimation
CABasicAnimation是核心動畫中的一個類,其父類是CAPropertyAnimation单刁,其子類是CASpringAnimation所刀,它的祖父是CAAnimation睹欲。它主要用于制作比較單一的動畫泣崩,例如昔头,平移、縮放考杉、旋轉、顏色漸變舰始、邊框的值的變化等崇棠,也就是將layer的某個屬性值從一個值到另一個值的變化。
2: 屬性說明
屬性 | 說明 |
---|---|
duration | 動畫的時長 |
beginTime | 指定動畫開始的時間 |
repeatCount | 重復的次數(shù)(PS:一直重復為 HUGE_VALF) |
repeatDuration | 設置動畫的時間 |
timingFunction | 動畫的速度變化[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut] |
autoreverses | 動畫結束時是否執(zhí)行逆動畫 |
fromValue | 屬性的起始值 |
toValue | 屬性的結束時的值 |
byValue | 所改變屬性相同起始值的改變量 |
3: 實例化
/* Creates a new animation object with its `keyPath' property set to
* 'path'.
創(chuàng)建一個新的動畫丸卷,其“keyPath”為“路徑”枕稀。
*/
+ (instancetype)animationWithKeyPath:(nullable NSString *)path;
下面是對path比較全面的總結,每個keypath對應一種屬性值的變化,其中涉及到顏色變化的都必須使用CGColor萎坷,因為對應是對layer的屬性凹联。
#ifndef AnimationKeyPathName_h
#define AnimationKeyPathName_h
#import <Foundation/Foundation.h>
#pragma mark- CATransform3D Key Paths
/**
* @brief 旋轉x,y,z分別是繞x,y,z軸旋轉
*/
static NSString *kCARotation = @"transform.rotation";
static NSString *kCARotationX = @"transform.rotation.x";
static NSString *kCARotationY = @"transform.rotation.y";
static NSString *kCARotationZ = @"transform.rotation.z";
/**
* @brief 縮放x,y,z分別是對x,y,z方向進行縮放
*/
static NSString *kCAScale = @"transform.scale";
static NSString *kCAScaleX = @"transform.scale.x";
static NSString *kCAScaleY = @"transform.scale.y";
static NSString *kCAScaleZ = @"transform.scale.z";
/**
* @brief 平移x,y,z同上
*/
static NSString *kCATranslation = @"transform.translation";
static NSString *kCATranslationX = @"transform.translation.x";
static NSString *kCATranslationY = @"transform.translation.y";
static NSString *kCATranslationZ = @"transform.translation.z";
#pragma mark- 平面
/**
* @brief CGPoint中心點改變位置,針對平面
*/
static NSString *kCAPosition = @"position";
static NSString *kCAPositionX = @"position.x";
static NSString *kCAPositionY = @"position.y";
/**
* @brief CGRect
*/
static NSString *kCABoundsSize = @"bounds.size";
static NSString *kCABoundsSizeW = @"bounds.size.width";
static NSString *kCABoundsSizeH = @"bounds.size.height";
static NSString *kCABoundsOriginX = @"bounds.origin.x";
static NSString *kCABoundsOriginY = @"bounds.origin.y";
/**
* @brief 透明度
*/
static NSString *kCAOpacity = @"opacity";
/**
* @brief 背景色
*/
static NSString *kCABackgroundColor = @"backgroundColor";
/**
* @brief 圓角
*/
static NSString *kCACornerRadius = @"cornerRadius";
/**
* @brief 邊框
*/
static NSString *kCABorderWidth = @"borderWidth";
/**
* @brief 陰影顏色
*/
static NSString *kCAShadowColor = @"shadowColor";
/**
* @brief 偏移量CGSize
*/
static NSString *kCAShadowOffset = @"shadowOffset";
/**
* @brief 陰影透明度
*/
static NSString *kCAShadowOpacity = @"shadowOpacity";
/**
* @brief 陰影圓角
*/
static NSString *kCAShadowRadius = @"shadowRadius";
#endif /* AnimationKeyPathName_h */
4:簡單用法
4.1 位移動畫
//使用CABasicAnimation創(chuàng)建基礎動畫
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-75)];
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-75)];
animation.duration = 2.0f;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[_demoView.layer addAnimation:animation forKey:@"positionAnimation"];
ps:
//如果fillMode=kCAFillModeForwards和removedOnComletion=NO哆档,那么在動畫執(zhí)行完畢后蔽挠,圖層會保持顯示動畫執(zhí)行后的狀態(tài)。但在實質上瓜浸,圖層的屬性值還是動畫執(zhí)行前的初始值澳淑,并沒有真正被改變。
//animation.duration.fillMode = kCAFillModeForwards;
//animation.duration.removedOnCompletion = NO;
4.2 透明度動畫
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.fromValue = [NSNumber numberWithFloat:1.0f];
animation.toValue = [NSNumber numberWithFloat:0.2f];
animation.duration = 1.0f;
[_demoView.layer addAnimation:animation forKey:@"opacityAniamtion"];
4.3 縮放動畫
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];//同上
animation.toValue = [NSNumber numberWithFloat:2.0f];
animation.duration = 1.0f;
[_demoView.layer addAnimation:animation forKey:@"scaleAnimation"];
4.4 旋轉動畫
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//繞著z軸為矢量插佛,進行旋轉(@"transform.rotation.z"==@@"transform.rotation")
animation.toValue = [NSNumber numberWithFloat:M_PI];
animation.duration = 1.0f;
[_demoView.layer addAnimation:animation forKey:@"rotateAnimation"];
4.5 背景色變化動畫
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
animation.toValue =(id) [UIColor greenColor].CGColor;
animation.duration = 1.0f;
[_demoView.layer addAnimation:animation forKey:@"backgroundAnimation"];
4.6 組動畫
CABasicAnimation *positionAnima = [CABasicAnimation animationWithKeyPath:@"position.y"];
positionAnima.fromValue = @(self.imageView.center.y);
positionAnima.toValue = @(self.imageView.center.y-30);
positionAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
CABasicAnimation *transformAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
transformAnima.fromValue = @(0);
transformAnima.toValue = @(M_PI);
transformAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
CAAnimationGroup *animation = [CAAnimationGroup animation];
animaGroup.duration = 2.0f;
animaGroup.fillMode = kCAFillModeForwards;
animaGroup.removedOnCompletion = NO;
animaGroup.animations = @[positionAnima,transformAnima];
[_demoView.layer addAnimation:animaGroup forKey:@"Animation"];
5: 獲取動畫開始和結束時的事件
添加代理事件
animation.delegate = self;
ps:會有循環(huán)引用,暫未解決!
實現(xiàn)代理方法
//動畫開始時
- (void)animationDidStart:(CAAnimation *)anim
{
if ([anim isEqual:[self.imageView.layer animationForKey:@"Animation"]]) {
NSLog(@"動畫組執(zhí)行了");
}
}
//動畫結束時
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
//方法中的flag參數(shù)表明了動畫是自然結束還是被打斷,比如調用了removeAnimationForKey:方法或removeAnimationForKey方法杠巡,flag為NO,如果是正常結束雇寇,flag為YES氢拥。
NSLog(@"結束了");
}