3

中心を中心に NSView の回転をアニメーション化しようとしていますが、回転中に左右に移動し続けます。これは何が原因ですか?

-(void)startRefreshAnimation {

    [NSAnimationContext beginGrouping];
    [[NSAnimationContext currentContext] setDuration:1.0];
    [[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    [NSAnimationContext currentContext].completionHandler = ^{ [self startRefreshAnimation]; };
    [[view animator] setFrameCenterRotation:previousRotation - 90.0];
    previousRotation += -90.0;
    [NSAnimationContext endGrouping];

}

回転中のシフトアップ:

ここに画像の説明を入力

回転中のシフトダウン:

ここに画像の説明を入力

4

2 に答える 2

2

ここにダミープロジェクトと私が見つけた答えがあります。(ココア申請用)

NSImageView を回転する

github プロジェクト (ダウンロード)

于 2015-08-13T15:24:56.100 に答える
1

ドキュメントから:

アプリケーションがレイヤーの anchorPoint プロパティを変更した場合、動作は未定義です。Core Animation レイヤーを管理していないビューにこのメッセージを送信すると、例外が発生します。

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html

あなたのビューは、変更されていないアンカー ポイントを持つ CALayer を管理していますか?

編集

同様のコードをセットアップすると、まったく同じ結果が得られました。原点またはアンカー ポイントを調整しないと、この問題を解決できません。私の理論では、この特定のメソッドにはバグが含まれているか (自動レイアウトの場合)、予期しない方法で動作するというものです。を使用して正しい効果を達成しましたCABasicAnimation

/* setup */

....
     _view.layer.anchorPoint = CGPointMake(0.5f, 0.5f);
     _view.layer.position = ...

    [self startRefreshAnimation];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    [self startRefreshAnimation];
}

-(void)startRefreshAnimation {

    CABasicAnimation *anim2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    anim2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    anim2.fromValue = [NSNumber numberWithFloat:previousRotation * (M_PI / 180.0f)];
    anim2.toValue = [NSNumber numberWithFloat:(previousRotation + 90.0f) * (M_PI / 180.0f)];
    previousRotation = previousRotation + 90.0f;
    anim2.duration = 1.0f;
    anim2.delegate = self;
    [_view.layer addAnimation:anim forKey:@"transform"];
}
于 2013-07-11T15:11:32.080 に答える