美文网首页iOS 开发每天分享优质文章
iOS UIView 开始旋转,暂停,继续旋转

iOS UIView 开始旋转,暂停,继续旋转

作者: 心猿意码_ | 来源:发表于2022-04-06 16:03 被阅读0次
    • 先看下效果


      旋转.gif

    首选封装个分类

    #import <QuartzCore/QuartzCore.h>
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface CALayer (Category)
    // 旋转
    +(void) makeAnimation:(UIView *)view;
    // 暂停
    +(void)pauseLayer:(CALayer*)layer;
    // 继续
    +(void)resumeLayer:(CALayer*)layer;
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "CALayer+Category.h"
    
    @implementation CALayer (Category)
    
    // 旋转
    +(void) makeAnimation:(UIView *)view {
        CABasicAnimation *rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.removedOnCompletion = NO;
        rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI*2.0];
        rotationAnimation.duration = 4;
        rotationAnimation.repeatCount = HUGE_VALF;
        [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }
    
    // 暂停
    +(void)pauseLayer:(CALayer*)layer
    {
        CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
        layer.speed = 0.0;
        layer.timeOffset = pausedTime;
    }
    
    // 继续
    +(void)resumeLayer:(CALayer*)layer
    {
        CFTimeInterval pausedTime = [layer timeOffset];
        layer.speed = 1.0;
        layer.timeOffset = 0.0;
        layer.beginTime = 0.0;
        CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
        layer.beginTime = timeSincePause;
    }
    
    
    @end
    

    使用

    #import "ViewController.h"
    #import "CALayer+Category.h"
    
    @interface ViewController ()
    @property (nonatomic, weak) UIView *v;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIView *v = [[UIView alloc] init];
        v.frame = CGRectMake(0, 0, 100, 100);
        v.center = self.view.center;
        v.backgroundColor = [UIColor redColor];
        [self.view addSubview:v];
        self.v = v;
        
        UIButton *a = [UIButton buttonWithType:UIButtonTypeSystem];
        a.frame = CGRectMake(v.frame.origin.x, CGRectGetMaxY(v.frame) + 30, 50, 30);
        [a setTitle:@"旋转" forState:UIControlStateNormal];
        [a addTarget:self action:@selector(aClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:a];
        
        UIButton *b = [UIButton buttonWithType:UIButtonTypeSystem];
        b.frame = CGRectMake(v.frame.origin.x, CGRectGetMaxY(a.frame) + 30, 50, 30);
        [b setTitle:@"暂停" forState:UIControlStateNormal];
        [b addTarget:self action:@selector(bClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:b];
        
        UIButton *c = [UIButton buttonWithType:UIButtonTypeSystem];
        c.frame = CGRectMake(v.frame.origin.x, CGRectGetMaxY(b.frame) + 30, 50, 30);
        [c setTitle:@"继续" forState:UIControlStateNormal];
        [c addTarget:self action:@selector(cClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:c];
        
    }
    
    // 旋转
    - (void)aClick{
        [CALayer makeAnimation:self.v];
    }
    
    // 暂停
    - (void)bClick{
        [CALayer pauseLayer:self.v.layer];
    }
    
    // 继续
    - (void)cClick{
        [CALayer resumeLayer:self.v.layer];
    }
    
    @end
    

    Demo见链接↓
    https://gitee.com/shouny/rotating.git

    相关文章

      网友评论

        本文标题:iOS UIView 开始旋转,暂停,继续旋转

        本文链接:https://www.haomeiwen.com/subject/mhnrsrtx.html