54

iPhone の基本的な回転アニメーションがあります。ビューの位置が維持されるようにアニメーションを「一時停止」する方法はありますか? これを行う1つの方法は、アニメーションを「削除」する代わりに「完了する」ようにすることだと思いますが、どうすればよいですか?

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:rotationAnimation forKey:@"rotationAnimation"];
4

6 に答える 6

165

最近公開された 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 が導入されました。

于 2010-06-09T07:46:38.463 に答える
18

Swift 3 の回答:

クレジット@ウラジミール

コード:

 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
}
于 2016-11-03T11:20:30.383 に答える
7

ビューのレイヤーの現在の状態を の状態に合わせて設定しpresentationLayer、アニメーションを削除します。

CALayer *pLayer = [myView.layer presentationLayer];
myView.layer.transform = pLayer.transform;
[myView.layer removeAnimationForKey:@"rotationAnimation"];
于 2010-02-23T01:55:38.533 に答える
3

@Vladimir と @Supratik-Majumdar の功績

CALayer 拡張機能としての Swift 5.1

    extension CALayer
    {
        func pauseAnimation() {
            if isPaused() == false {
                let pausedTime = convertTime(CACurrentMediaTime(), from: nil)
                speed = 0.0
                timeOffset = pausedTime
            }
        }

        func resumeAnimation() {
            if isPaused() {
                let pausedTime = timeOffset
                speed = 1.0
                timeOffset = 0.0
                beginTime = 0.0
                let timeSincePause = convertTime(CACurrentMediaTime(), from: nil) - pausedTime
                beginTime = timeSincePause
            }
        }

        func isPaused() -> Bool {
            return speed == 0
        }
    }
于 2019-11-27T23:55:32.987 に答える
0

タイマーを使用するか、アニメーション デリゲート メソッドを処理できます。

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

それがあなたを助けることを願っています。

于 2011-12-12T08:02:48.567 に答える
-1

最も単純なもの

self.viewBall.layer.position = self.viewBall.layer.presentationLayer().position
于 2015-05-29T11:46:14.450 に答える