IOS開發核心動畫六:動畫組

 

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"position.y";
    anim.toValue = @400;
    
    /**
     *  1:CABasicAnimation:主要用於縮放,平移,轉場CATransiton動畫主要用於場景切換,CAKeyFrameAnimation:關鍵幀動畫,主要用於圖標抖動等
       2:在CABasicAnimation中,設置anim.removedOnCompletion,目的是動畫結束後不刪除動畫,anim.fillMode = kCAFillModeForwards;設置動畫的模式,設置這兩個屬性,能夠不讓動畫執行結束後,恢復到原來的位置,而是保留結束動畫的狀態
       3:CAAnimationGroup:她會自動執行數組animations裏的動畫
     
     */
    
    
    
    
//    anim.removedOnCompletion = NO;
//    anim.fillMode = kCAFillModeForwards;
//    
//    [self.redView.layer addAnimation:anim forKey:nil];
    
    
    CABasicAnimation *anim2 = [CABasicAnimation animation];
    anim2.keyPath = @"transform.scale";
    anim2.toValue = @0.5;
//    anim2.removedOnCompletion = NO;
//    anim2.fillMode = kCAFillModeForwards;
//    [self.redView.layer addAnimation:anim2 forKey:nil];
    

    CAAnimationGroup *group = [CAAnimationGroup animation];
    //會自動執行animations數組當中全部的動畫對象
    group.animations = @[anim,anim2];
    
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    [self.redView.layer addAnimation:group forKey:nil];
    
}



@end

能夠同時執行多個動畫.數組

建立組動畫動畫

    CAAnimationGroup *group = [CAAnimationGroup animation];atom

    

    平移spa

    CABasicAnimation *anim = [CABasicAnimation animation];code

    anim.keyPath = @"position.y";orm

    anim.toValue = @400;對象

    

    縮放blog

    CABasicAnimation *scaleAnim = [CABasicAnimation  animation];rem

    scaleAnim.keyPath = @"transform.scale";animation

    scaleAnim.toValue = @0.5;

    設置動畫組屬性

    group.animations = @[anim,scaleAnim];

    

    group.removedOnCompletion = NO;

    group.fillMode = kCAFillModeForwards;

    添加組動畫

    [self.redView.layer addAnimation:group forKey:nil];

 

使用動畫組的好處,不須要每次都去添加動畫,設置動畫完成時的屬性.

只須要把要執行的動畫,添加到動畫組的animations數組當中便可,

最後把組動畫添加到層上面,就會自動執行數組當中的動畫.

動畫完成時設置的屬性也只須要設置一次.

相關文章
相關標籤/搜索