1. 程式人生 > >IOS -------Transfrom屬性動畫

IOS -------Transfrom屬性動畫

1.建立一個檢視

@property (nonatomic, strong) UIView *redView;
self.redView = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
    _redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_redView];

2.動畫設定方法

- (void)test{
    [UIView animateWithDuration:2 animations:^{
        
    }];
    
    [UIView animateWithDuration:2 animations:^{
        //具體動畫的內容
    } completion:^(BOOL finished) {
        //動畫完成之後的一些工作
    }];
}

3.建立動畫

- (void)test1{
    //CGAffineTransformIdentity 清楚之前所有的狀態 還原到最初狀態
    //make對應的方法只執行一次
    //沒有make的可以執行多次
    //tx ty偏移值
    [UIView animateWithDuration:1 animations:^{
        //平移
        self.redView.transform = CGAffineTransformMakeTranslation(100, 100);
        self.redView.transform = CGAffineTransformTranslate(self.redView.transform, 50, 0);
        
       //縮放
        self.redView.transform = CGAffineTransformMakeScale(2, 1);
        self.redView.transform = CGAffineTransformScale(self.redView.transform, 0.01, 1);
        
        //旋轉
        self.redView.transform = CGAffineTransformMakeRotation(M_PI_2);
        self.redView.transform = CGAffineTransformRotate(self.redView.transform, M_PI_2);
    }];
}

4.動畫開始

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 
    [NSTimer scheduledTimerWithTimeInterval:0.01 repeats:YES block:^(NSTimer * _Nonnull timer) {
        self.redView.transform = CGAffineTransformRotate(self.redView.transform, 2/180.0*M_PI);
    }]; 
    [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [UIView animateWithDuration:0.2 animations:^{
            self.redView.transform = CGAffineTransformScale(self.redView.transform, 1.5, 1.5);
        } completion:^(BOOL finished) {
            self.redView.transform = CGAffineTransformScale(self.redView.transform, 0.65, 0.65);
            self.redView.transform = CGAffineTransformIdentity;
        }];
        //self.redView.transform = CGAffineTransformIdentity;
    }]; 
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(jump) userInfo:nil repeats:YES];
}

5.jump動畫

- (void)jump{
    [UIView animateWithDuration:0.2 animations:^{
        self.redView.transform = CGAffineTransformScale(self.redView.transform, 1.5, 1.5);
    } completion:^(BOOL finished) {
        self.redView.transform = CGAffineTransformScale(self.redView.transform, 0.65, 0.65);
        self.redView.transform = CGAffineTransformIdentity;
    }];
}