YoYoBall加载动画

original url

note the code

@interface YoYoView() {
    CAShapeLayer *_ringLayer;
    CAShapeLayer *_ballLayer;
    CAShapeLayer *_ballContentLayer;
}
@end
@implementation YoYoView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
        [self startAnimation];
    }
    return self;
}

- (void)createUI {
    CGFloat ringLayerRadius = 73;
    CGFloat ballLayerRadius = 11;
    CGFloat ballContentLayerWidth = 232;
    CGRect ringLayerRect = CGRectMake(0, 0, 2 * ringLayerRadius, 2 * ringLayerRadius);
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:ringLayerRect];
    
    _ringLayer = [CAShapeLayer layer];
    _ringLayer.path = path.CGPath;
    _ringLayer.lineWidth = 22;
    _ringLayer.strokeColor = [UIColor redColor].CGColor;
    _ringLayer.frame = ringLayerRect;
    _ringLayer.fillColor = [UIColor clearColor].CGColor;
    _ringLayer.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
    [self.layer addSublayer:_ringLayer];
    
    _ballContentLayer = [CAShapeLayer layer];
    _ballContentLayer.frame = CGRectMake(0, 0, ballContentLayerWidth, ballContentLayerWidth);
    _ballContentLayer.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
    [self.layer addSublayer:_ballContentLayer];
    
    [self initRotate];
    
    _ballLayer = [CAShapeLayer layer];
    _ballLayer.frame = CGRectMake(0, 0,  2 * ballLayerRadius, 2 * ballLayerRadius);
    _ballLayer.cornerRadius = ballLayerRadius;
    _ballLayer.backgroundColor = [UIColor redColor].CGColor;
    _ballLayer.position = CGPointMake(ballContentLayerWidth / 2, 0);
    [_ballContentLayer addSublayer:_ballLayer];
}

- (void)initRotate {
    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2 / 2);
    _ballContentLayer.affineTransform = transform;
}

- (void)startAnimation {
    CAMediaTimingFunction *timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.79 :0.14 :0.123 :0.83];
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform.rotation";
    animation.duration = 2.5;
    animation.repeatCount = INFINITY;
    animation.byValue = @(M_PI * 2);
    animation.timingFunction = timingFunction;
    [_ballContentLayer addAnimation:animation forKey:animation.keyPath];
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa阅读 8,925评论 0 6
  • 小兔是个可爱的姑娘,虽然她的年龄并不可爱了,可她好奇心极强,折腾因子遍布全身,“不折腾不成活”常挂在嘴边。 那年的...
    小小Sera阅读 314评论 0 0
  • 2017年3.19(像以前一样,突然想写日记了) 假期补课,老师津津乐道的讲着她的经济改革,持续了三个钟头,要说大...
    乐_f7e0阅读 197评论 0 0