speed(CAMediaTiming)控制動畫暫停和結(jié)束

QQ20170818-151734.gif
//
//  ViewController.m
//  MediaTiming
//
//  Created by apple on 17/8/18.
//  Copyright ? 2017年 Wang. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) CALayer *layer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CALayer *layer = [CALayer layer];
    layer.backgroundColor = [UIColor purpleColor].CGColor;
    layer.frame = CGRectMake(0, 0, 100, 100);
    [self.view.layer addSublayer:layer];
    self.layer = layer;
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    basicAnimation.duration = 4.f;
    basicAnimation.repeatCount = HUGE;
    basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(50, 400)];
    [layer addAnimation:basicAnimation forKey:nil];
}

- (void) pauseLayer: (CALayer *) theLayer
{
    CFTimeInterval mediaTime = CACurrentMediaTime();
    CFTimeInterval pausedTime = [theLayer convertTime: mediaTime fromLayer: nil];
    theLayer.speed = 0.0;
    theLayer.timeOffset = pausedTime;
}

//-----------------------------------------------------------------------------

- (void) removePauseForLayer: (CALayer *) theLayer;
{
    theLayer.speed = 1.0;
    theLayer.timeOffset = 0.0;
    theLayer.beginTime = 0.0;
}

//-----------------------------------------------------------------------------

- (void) resumeLayer: (CALayer *) theLayer;
{
    CFTimeInterval pausedTime = [theLayer timeOffset];
    [self removePauseForLayer: theLayer];
    CFTimeInterval mediaTime = CACurrentMediaTime();
    CFTimeInterval timeSincePause =
    [theLayer convertTime: mediaTime fromLayer: nil] - pausedTime;
    theLayer.beginTime = timeSincePause;
}

- (IBAction)pauseClick:(UIButton *)sender {
    [self pauseLayer:self.layer];
    
}

- (IBAction)resumeClick:(UIButton *)sender {
    [self resumeLayer:self.layer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

參考:How To Pause & Resume Animation - KDCircularProgress Circular Bar

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

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

  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,847評論 2 45
  • 兩個多月前,我與早餐結(jié)下了不解之緣。做早餐這件事,我相信這將是我愿意持續(xù)做的事情之一。 有一天與辦公室的小伙伴閑聊...
    阿梓妹88閱讀 380評論 0 1
  • 每次都毫無邏輯也毫無可讀性地自說自話,這里仿佛變成了一個巨大的垃圾桶,因為沒人知道我是誰,大概也沒人會看,可以肆無...
    流浪的小刺猬_閱讀 425評論 0 2
  • 看完歡樂頌2了??纯次迕赖纳?,想想她們的經(jīng)歷,性格,遭遇,邱瑩瑩這個角色最喜歡。傻氣,骨子里有一種孤勇。 ...
    愛吃大米飯的羊閱讀 247評論 2 2
  • 前些天,看了80后創(chuàng)業(yè)者熊七公發(fā)布的誓言,每天堅持至少1篇圖文,深有感悟。 創(chuàng)業(yè)者如此之忙,還有如此情懷。...
    周脖通閱讀 1,316評論 0 2

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