1. 程式人生 > 其它 >iOS開發_讓UIButton上的image進行360度旋轉

iOS開發_讓UIButton上的image進行360度旋轉

1、目的

  • 為了按鈕上的圖片有一個360度動畫旋轉效果。

2、程式碼

- (void)changeAction:(UIButton *)changeBtn {
    
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 0.6;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 2;
    [changeBtn.imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

3、呼叫

UIButton *refreshButton = [[UIButton alloc] init];
[self.view addSubview:refreshButton];
[refreshButton setImage:[UIImage imageNamed:@"icon_refresh"] forState:UIControlStateNormal];
[refreshButton makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.view).offset(-30);
    make.bottom.equalTo(settingButton);
    make.width.height.equalTo(60);
}];

[refreshButton addTarget:self action:@selector(changeAction:) forControlEvents:UIControlEventTouchUpInside];

4、效果