YoYoBall加載動(dòng)畫(huà)

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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,578評(píng)論 19 139
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 9,055評(píng)論 0 6
  • 小兔是個(gè)可愛(ài)的姑娘,雖然她的年齡并不可愛(ài)了,可她好奇心極強(qiáng),折騰因子遍布全身,“不折騰不成活”常掛在嘴邊。 那年的...
    小小Sera閱讀 355評(píng)論 0 0
  • 2017年3.19(像以前一樣,突然想寫(xiě)日記了) 假期補(bǔ)課,老師津津樂(lè)道的講著她的經(jīng)濟(jì)改革,持續(xù)了三個(gè)鐘頭,要說(shuō)大...
    樂(lè)_f7e0閱讀 218評(píng)論 0 0

友情鏈接更多精彩內(nèi)容