CAKeyframeAnimation跟CABasicAnimation的區別是:CABasicAnimation只能從一個數值(fromValue)變到另外一個數值(toValue),而CAKeyframeAnimation會使用一個NSArray保存這些數值動畫
- (void)value { CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; // 設置動畫屬性 anim.keyPath = @"position"; NSValue *v1 = [NSValue valueWithCGPoint:CGPointZero]; NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(160, 160)]; NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(270, 0)]; anim.values = @[v1,v2,v3]; anim.duration = 2; [_redView.layer addAnimation:anim forKey:nil]; } -(void)path { CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; // 設置動畫屬性 anim.keyPath = @"position"; UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)]; anim.path = path.CGPath; anim.duration = 0.25; // 取消反彈 anim.removedOnCompletion = NO; anim.fillMode = kCAFillModeForwards; anim.repeatCount = MAXFLOAT; [_redView.layer addAnimation:anim forKey:nil]; }
keyPath能夠使用的key #define angle2Radian(angle) ((angle)/180.0*M_PI) transform.rotation.x 圍繞x軸翻轉 參數:角度 angle2Radian(5) transform.rotation.y 圍繞y軸翻轉 參數:同上 transform.rotation.z 圍繞z軸翻轉 參數:同上 transform.rotation 默認圍繞z軸 transform.scale.x x方向縮放 參數:縮放比例 1.5 transform.scale.y y方向縮放 參數:同上 transform.scale.z z方向縮放 參數:同上 transform.scale 全部方向縮放 參數:同上 transform.translation.x x方向移動 參數:x軸上的座標 100 transform.translation.y x方向移動 參數:y軸上的座標 transform.translation.z x方向移動 參數:z軸上的座標 transform.translation 移動 參數:移動到的點 (100,100) opacity 透明度 參數:透明度 0.5 backgroundColor 背景顏色 參數:顏色 (id)[[UIColor redColor] CGColor] cornerRadius 圓角 參數:圓角半徑 5 borderWidth 邊框寬度 參數:邊框寬度 5 bounds 大小 參數:CGRect contents 內容 參數:CGImage contentsRect 可視內容 參數:CGRect 值是0~1之間的小數 hidden 是否隱藏 position shadowColor shadowOffset shadowOpacity shadowRadius