开发者

Cocoa - Core Animation - How to stop proxy animation?

开发者 https://www.devze.com 2023-02-11 04:51 出处:网络
I have an NSWindow which I fade in from invisible to full opacity, over a custom time, using the animator proxy:

I have an NSWindow which I fade in from invisible to full opacity, over a custom time, using the animator proxy:

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:1.0f]; // Custom timing, 1 sec.
[[myWindow animator] setAlphaValue: 1.0f];
[NSAnimationContext endGrouping];

However, if I attempt to set the window's visibility while the animation is proceeding, the animation is not stopped. In the following example, the window will briefly appear at 0.5 visibility, but will then continue to animate.开发者_如何学Go

e.g.

[myWindow setAlphaValue: 0.5f];  // Animation continues after calling this.

Q. How can I stop the animation?

Thanks.


I've got an application that does pretty much this (the menu bar covering window in Shroud) and answering this question I found a bug in it—although my animations are 0.1s so it would probably never be triggered in practice. But thanks. :-)

Animation durations of 0s are special-cased to behave just like setting the alpha value directly, so you can't use them, but you can use a very small duration, something like this, which will create a new animation that supersedes the in-progress one:

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.01];
[[myWindow animator] setAlphaValue:0.5];
[NSAnimationContext endGrouping];


Alternative approach is to provide custom animation class (e.g. CABasicAnimation) with the delegate property by setting the animations property. Then the corresponding delegate methods can be used to track and manipulate actual animations.

0

精彩评论

暂无评论...
验证码 换一张
取 消