我试图让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];
}在旋转过程中向上移动:

在旋转过程中向下移动:

发布于 2013-07-11 23:11:32
从文档中:
如果应用程序更改了图层的anchorPoint属性,则行为未定义。将此消息发送到未管理核心动画层的视图会导致异常。
您的视图是否正在管理具有未修改锚点的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"];
}发布于 2015-08-13 23:24:56
https://stackoverflow.com/questions/17596916
复制相似问题