gpt4 book ai didi

cocoa - NSWindow 翻转动画 - 像 iWork

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

我正在尝试实现与 iWork 中相同的窗口翻转 -

https://dl.dropbox.com/u/2338382/Window%20Flipping.mov

但是,我似乎无法找到一种简单的方法来做到这一点。一些教程建议将窗口两侧的快照图像粘贴在更大的透明窗口中,并对它们进行动画处理。这可能可行,但看起来有点老套,而且示例代码总是臃肿。一些教程建议使用私有(private) API,并且由于此应用程序可能受 MAS 限制,因此我想避免这种情况。

我应该如何实现这个?有人有任何提示吗?

NSWindow+翻转

我已将下面链接的古老代码重写为 NSWindow+Flipping。您可以从 my misc. Cocoa collection on GitHub, PCSnippets 获取这些源文件.

最佳答案

您可以使用CoreGraphics框架来实现这一点。看看这个:

- (void) flipWithDuration: (float) duration forwards: (BOOL) forwards
{
CGSTransitionSpec spec;
CGSTransitionHandle transitionHandle;
CGSConnection cid = CGSDefaultConnection;

spec.type = CGSFlip;
spec.option = 0x80 | (forwards ? 2 : 1);
spec.wid = [self windowNumber];
spec.backColor = nil;

transitionHandle = -1;
CGSNewTransition (cid, &spec, &transitionHandle);
CGSInvokeTransition (cid, transitionHandle, duration);
[[NSRunLoop currentRunLoop] runUntilDate:
[NSDate dateWithTimeIntervalSinceNow: duration]];
CGSReleaseTransition (cid, transitionHandle);
}

您可以下载示例项目:here 。更多信息here .

更新:

看看this project 。这实际上就是您所需要的。

About this project:

This category on NSWindow allows you to switch one window for another, using the "flip" animation popularized by Dashboard widgets. This was a nice excuse to learn something about CoreImage and how to use it in Cocoa. The demo app shows how to use it. Scroll to the end to see what's new in this version!

Basically, all you need to do is something like:

[someWindow flipToShowWindow:someOtherWindow forward:YES];

However, this code makes some assumptions: — someWindow (the initial window) is already visible on-screen. — someOtherWindow (the final window) is not already visible on-screen. — Both windows can be resized to the same size, and aren't too large or complicated — the latter conditions being less important the faster your CPU/video card is. — The windows won't go away while the animation is running. — The user won't try to click on the animated window or do something while the animation is running.

The implementation is quite straightforward. I move the final to the same position and size as the initial window. I then position a larger transparent window so it covers that frame. I render both window contents into CIImages, hide both windows, and start the animation. Each frame of the animation renders a perspective-distorted image into the transparent window. When the animation is done, I show the final window. Some tricks are used to make this faster; the flipping window is setup only once; the final window is hidden by setting its alpha to 0.0, not by ordering it out and later ordering it back in again, for instance.

The main bottleneck is the CoreImage filter, and the first frame always takes much longer to render — 4 or 6 times what it takes for the remaining frames. I suppose this time is spent with setup and downloading to the video card. So I calculate the time this takes and draw a second frame at a stage where the rotation begins to show. The animation begins at this point, but, if those first two frames took too long, I stretch the duration to make sure that at least 5 more frames will get rendered. This will happen with slow hardware or large windows. At the end, I don't render the last frame at all and swap the final window in instead.

关于cocoa - NSWindow 翻转动画 - 像 iWork,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11312725/

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