ios-day18-09(使用CAKeyframeAnimation實現對UIView在多個位置之間平移、沿著某一條路徑平移)
阿新 • • 發佈:2019-02-18
// // JLViewController.m // 09-核心動畫 // // Created by XinYou on 15-4-2. // Copyright (c) 2015年 vxinyou. All rights reserved. // #import "JLViewController.h" @interface JLViewController () @property (weak, nonatomic) IBOutlet UIImageView *iconView; - (IBAction)multiPosition; - (IBAction)surroundCircle; - (IBAction)back; @end @implementation JLViewController - (void)viewDidLoad { [super viewDidLoad]; } /** * 平移到多個不同的位置 */ - (IBAction)multiPosition { // 1,建立動畫 CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; // 2,設定動畫物件 // 設定CALayer要修改的屬性 anim.keyPath = @"position"; NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(160, 90)]; NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(160, 190)]; NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(260, 190)]; NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(260, 90)]; // 設定position屬性的每一幀要改變的值 anim.values = @[v1, v2, v3, v4]; // 設定動畫持續時間 anim.duration = 2.0; // 保持動畫執行完畢後的狀態 anim.removedOnCompletion = NO; anim.fillMode = kCAFillModeForwards; // 3,新增動畫 [self.iconView.layer addAnimation:anim forKey:nil]; } /** * 繞圓平移 */ - (IBAction)surroundCircle { // 1,建立動畫 CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; // 2,設定動畫物件 anim.keyPath = @"position"; anim.removedOnCompletion = NO; anim.fillMode = kCAFillModeForwards; anim.duration = 2.0; // 建立路徑 CGMutablePathRef path = CGPathCreateMutable(); CGPathAddEllipseInRect(path, NULL, CGRectMake(90, 90, 100, 100)); // 設定路徑 anim.path = path; CGPathRelease(path); // 3,新增動畫 [self.iconView.layer addAnimation:anim forKey:nil]; } /** * 還原 */ - (IBAction)back { [self.iconView.layer removeAllAnimations]; } @end