亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法暫停CABasicAnimation?

有沒有辦法暫停CABasicAnimation?

繁星coding 2019-11-08 12:51:52
我有一個基本的iPhone旋轉動畫。有什么方法可以“暫?!眲赢?,以便維持視圖的位置?我想這樣做的一種方法是使動畫“完成”,而不是對其調用“刪除”,我該怎么做?CABasicAnimation* rotationAnimation;rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2];rotationAnimation.duration = 100;rotationAnimation.cumulative = YES;rotationAnimation.repeatCount = HUGE_VALF;rotationAnimation.removedOnCompletion = NO;rotationAnimation.fillMode = kCAFillModeForwards;[myView.layer addAnimation:rotationAn
查看完整描述

3 回答

?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

最近出現的Apple技術說明QA1673描述了如何暫停/恢復圖層的動畫。


暫停和恢復動畫列表如下:


-(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;

}

編輯: iOS 10引入了新的API-UIViewPropertyAnimator,該API可以更交互地處理動畫,例如,它使暫停和恢復動畫或“搜索”到特定進度值變得容易。


查看完整回答
反對 回復 2019-11-08
?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

回答Swift 3:


積分@Vladimir


碼:


 func pauseAnimation(){

  let pausedTime = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)

  layer.speed = 0.0

  layer.timeOffset = pausedTime

}


func resumeAnimation(){

  let pausedTime = layer.timeOffset

  layer.speed = 1.0

  layer.timeOffset = 0.0

  layer.beginTime = 0.0

  let timeSincePause = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime

  layer.beginTime = timeSincePause

}


查看完整回答
反對 回復 2019-11-08
?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

您可以使用計時器或處理動畫委托方法:


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

這是我的代碼:


// ...

[self startAnimation];

// ...


- (void)startAnimation {

CABasicAnimation* rotationAnimation;

rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

rotationAnimation.fromValue = [NSNumber numberWithFloat:0];

rotationAnimation.toValue = [NSNumber numberWithFloat: M_2_PI];

rotationAnimation.duration = 1.0;

rotationAnimation.cumulative = YES;

// rotationAnimation.repeatCount = 0; // <- if repeatCount set to infinite, we'll not receive the animationDidStop notification when the animation is repeating

rotationAnimation.removedOnCompletion = NO;

rotationAnimation.fillMode = kCAFillModeForwards;

rotationAnimation.delegate = self; // <- hanlde the animationDidStop method

[myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];


}


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {

if (shouldContinueAnimation) // <- set a flag to start/stop the animation

    [self startAnimation];

}

希望它能對您有所幫助。


查看完整回答
反對 回復 2019-11-08
  • 3 回答
  • 0 關注
  • 2061 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號