近期做的东西中,要为一个有特定图片的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旋转动画中,方向可能有点不同,有点奇怪。

当中一些执行截图例如以下:

iOS 特定图片的button的旋转动画-LMLPHP

iOS 特定图片的button的旋转动画-LMLPHP

Demo地址:点击打开链接

05-11 20:29