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 回答