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,991評論 19 139
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 8,925評論 0 6
  • 小兔是個可愛的姑娘,雖然她的年齡并不可愛了,可她好奇心極強,折騰因子遍布全身,“不折騰不成活”常掛在嘴邊。 那年的...
    小小Sera閱讀 314評論 0 0
  • 2017年3.19(像以前一樣,突然想寫日記了) 假期補課,老師津津樂道的講著她的經濟改革,持續了三個鐘頭,要說大...
    樂_f7e0閱讀 197評論 0 0