gpt4 book ai didi

objective-c - CABasicAnimation 向左移动帧 300px

转载 作者:行者123 更新时间:2023-12-03 16:29:15 25 4
gpt4 key购买 nike

我需要 CABasicAnimation 方面的帮助。我正在尝试将 NSView 向左移动 300 像素。我发现这个线程:How to animate the frame of an layer with CABasicAnimation?

事实证明,对框架进行动画处理是不可能的,其中一个答案指向 Apple 网站上的 QA 链接,但它会将我带到一个通用页面: http://developer.apple.com/library/mac/#qa/qa1620/_index.html

那么,我怎样才能做一些像翻译 NSView/CALyer 这样简单的事情呢?谢谢!

最佳答案

NSView 有一个名为 NSAnimatablePropertyContainer 的协议(protocol),它允许您为 View 创建基本动画:

The NSAnimatablePropertyContainer protocol defines a way to add animation to an existing class with a minimum of API impact ... Sending of key-value-coding compliant "set" messages to the proxy will trigger animation for automatically animated properties of its target object.

NSAnimatablePropertyContainer 协议(protocol)可以在 here 找到

我最近使用此技术来更改帧的原点:

-(void)setOrigin:(NSPoint)aPoint {
[[self animator] setFrameOrigin:aPoint];
}

我没有调用 [view setFrameOrigin:],而是创建了另一个名为 setOrigin: 的方法,然后该方法将 setFrameOrigin: 调用应用于 View 的动画制作器。

如果你需要改变动画的持续时间,你可以这样做(类似于CATransactions):

-(void)setOrigin:(NSPoint)aPoint {
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setCompletionHandler:^{
...Completion Callback Code goes here...
}];
[[NSAnimationContext currentContext] setDuration:1.0];
[[self animator] setFrameOrigin:aPoint];
[NSAnimationContext endGrouping];
}

描述了 NSAnimationContext here

关于objective-c - CABasicAnimation 向左移动帧 300px,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9662692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com