无限旋转

废话不多说,直接上代码

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(60, 10, 222, 66);
    [button setTitle:@"点我" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickTest:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor=[UIColor orangeColor];
    [self.view addSubview:button];
    
    _imgV=[[UIImageView alloc]initWithFrame:CGRectMake(60, button.bottom+40, WIDTH-120, WIDTH-120)];
    _imgV.image=[UIImage imageNamed:@"mohuImage.png"];
    _imgV.contentMode=UIViewContentModeScaleAspectFill;
    _imgV.layer.masksToBounds=YES;
    _imgV.layer.cornerRadius=(WIDTH-120)/2.0;
    [self.view addSubview:_imgV];
    
    [_imgV.layer removeAnimationForKey:@"rotation"];
    CABasicAnimation *animation = [[CABasicAnimation alloc] init];
    animation.fromValue = @(0);
    animation.toValue = @(M_PI * 2);
    animation.duration = 6;
    animation.keyPath = @"transform.rotation.z";
    animation.repeatCount = NSIntegerMax;
    animation.removedOnCompletion = NO;
    [_imgV.layer addAnimation:animation forKey:@"rotation"];
- (void)clickTest:(UIButton*)button {
    if (button.selected) {
        [self resumeAnimate];
    }else {
        [self pauseAnimate];
    }
    button.selected=!button.selected;
}
- (void)pauseAnimate
{
    CFTimeInterval pausedTime = [_imgV.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    _imgV.layer.speed = 0.0;
    _imgV.layer.timeOffset = pausedTime;
}

- (void)resumeAnimate
{
    CFTimeInterval pausedTime = [_imgV.layer timeOffset];
    _imgV.layer.speed = 1.0;
    _imgV.layer.timeOffset = 0.0;
    _imgV.layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [_imgV.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    _imgV.layer.beginTime = timeSincePause;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • <!DOCTYPE html> .ig{-webkit-animation:circle 1.5s in...
    lily_flight阅读 5,543评论 0 1
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,084评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,613评论 4 61
  • 谁的青春不迷茫 普通人的生活大都是一样的,希望你做个有趣的普通人。
    王晶同学阅读 1,436评论 0 0
  • 自我认知 认识到自我与他人的不同, 自己才最了解自己, 抛开外人的评价给,自我认知一次
    Sasuke23阅读 1,180评论 0 0

友情链接更多精彩内容