使用NSTimer和CGAffineTransformMakeRotation实现旋转动画
首先定义需要用到的变量
float angle;
NSTimer* timer;
#pragma mark ------------------->旋转图片<-------------------
- (void)_doRotateImage{
//演员初始化
UIImageView* ivImage = [[UIImageView alloc] init];
UIImage* img = [UIImage imageNamed:@"image_loading"];
ivImage.image=img;
ivImage.frame=CGRectMake(100, 200, img.size.width, img.size.height);
ivImage.center=self.view.center;
ivImage.userInteractionEnabled=YES;
ivImage.tag=9999;
[self.view addSubview:ivImage];
//初始化定时器NSTimer
timer =[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(_doOnTimer) userInfo:nil repeats:YES];
//添加一个按钮用来暂停、开始定时器
UIButton* btnTimer = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnTimer setTitle:@"暂停" forState:UIControlStateNormal];
[btnTimer setTitle:@"开启" forState:UIControlStateSelected];
[btnTimer addTarget:self action:@selector(_doPauseOrStartTimer:) forControlEvents:UIControlEventTouchUpInside];
btnTimer.frame=CGRectMake(100, 340, 100, 40);
[self.view addSubview:btnTimer];
}
#pragma mark 定时器的触发事件
-(void) _doOnTimer{
UIView* view = [self.view.window viewWithTag:9999];
angle = angle + 0.05;//angle角度 double angle;
if (angle > 6.28) {//大于 M_PI*2(360度) 角度再次从0开始
angle = 0;
}
CGAffineTransform transform=CGAffineTransformMakeRotation(angle);
view.transform = transform;
}
-(void) _doPauseOrStartTimer:(UIButton*) sender{
if (sender.selected) {
[timer setFireDate:[NSDate distantPast]];
}else{
[timer setFireDate:[NSDate distantFuture]];
}
sender.selected=!sender.selected;
}
效果图如下:
标签:动画,angle,sender,selected,transform,timer,NSTimer,CGAffineTransformMakeRotation From: https://blog.51cto.com/u_16160131/6473911