我正在尝试滑入NSView
使用核心动画。我认为我需要使用显式动画而不是依赖于[[view animator] setFrame:newFrame]
. 这主要是因为我需要设置动画委托,以便在动画完成后采取行动。
我使用动画师让它工作得很好,但正如我所说,我需要在动画完成时得到通知。我的代码目前看起来像:
// Animate the controlView
NSRect viewRect = [controlView frame];
NSPoint startingPoint = viewRect.origin;
NSPoint endingPoint = startingPoint;
endingPoint.x += viewRect.size.width;
[[controlView layer] setPosition:NSPointToCGPoint(endingPoint)];
CABasicAnimation *controlPosAnim = [CABasicAnimation animationWithKeyPath:@"position"];
[controlPosAnim setFromValue:[NSValue valueWithPoint:startingPoint]];
[controlPosAnim setToValue:[NSValue valueWithPoint:endingPoint]];
[controlPosAnim setDelegate:self];
[[controlView layer] addAnimation:controlPosAnim forKey:@"controlViewPosition"];
这在视觉上是有效的(最后我会收到通知),但看起来实际的 controlView 并没有被移动。如果我让窗口刷新,controlView 就会消失。我尝试更换
[[controlView layer] setPosition:NSPointToCGPoint(endingPoint)];
和
[controlView setFrame:newFrame];
这确实会导致视图(和图层)移动,但它正在破坏某些东西,以至于我的应用程序很快就会因段错误而死掉。
大多数显式动画的例子似乎只是在移动一个CALayer
. 必须有一种方法可以移动NSView
并且还能够设置委托。任何帮助,将不胜感激。