iOS 特定圖片的button的旋轉動畫


近期做的東西中,要為一個有特定圖片的button加入旋轉動畫,Demo代碼例如以下:

#import "ViewController.h"

@interface ViewController () {
    BOOL flag;
}

@property (strong, nonatomic) UIImageView *imageView;

@end

@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];
    
    flag = YES;
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
    UIImage *aImage = [UIImage imageNamed:@"down.png"];
    [_imageView setImage:aImage];
    _imageView.center = self.view.center;
    [self.view addSubview:_imageView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"旋轉" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(rotate:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(110, 400, 100, 44);
    [self.view addSubview:button];
}

- (void)rotate:(id)sender {
    if (flag) {
        [UIView animateWithDuration:0.5 animations:^{
            _imageView.transform = CGAffineTransformMakeRotation(M_PI);
        } completion:^(BOOL finished) {
            flag = NO;
        }];
    }
    else {
        [UIView animateWithDuration:0.5 animations:^{
            _imageView.transform = CGAffineTransformMakeRotation(0);
        } completion:^(BOOL finished) {
            flag = YES;
        }];
    }
}

@end

執行時真機設備和模擬器的button旋轉動畫中,方向可能有點不同,有點奇怪。

當中一些執行截圖例如以下:


Demo地址:點擊打開鏈接




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM