如何捕获当前视图截图并在代码中重用? (iPhone SDK) [英] How to capture current view screenshot and reuse in code? (iPhone SDK)

查看:153
本文介绍了如何捕获当前视图截图并在代码中重用? (iPhone SDK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户旋转设备时,我正准备从一个UIView转换到另一个UIView。这本身并不困难。但是,由于我在旋转后显示完全不同的内容,因此UIKit提供的默认动画(旋转当前显示的视图)在概念上是不合适的。

I am attemting to transition from one UIView to another, when the user rotates the device. This, in of itself, is not difficult. However, since I am displaying completely different content after the rotation, the default animation provided by UIKit (rotating the currently displayed view) is inappropriate conceptually.

简单地禁用动画和突然间交换视图是可以忍受的,但远远低于我正在构建到应用程序其余部分的润色。我更喜欢这样做:

Simply disabling the animation and swapping the views suddenly is tolerable, but is far below the polish I'm building into the rest of the app. What I would prefer to do is this:

当调用shouldAutorotateToInterfaceOrientation:时,我想获取用户视图的不透明视图快照,如果你愿意的话在轮换之前。然后在完成旋转并且系统已应用视图转换等之后,我可以显示我保存的快照视图,并将我选择的转换设置为我的新视图的动画。完成后,我可以发布我的快照并继续前进。

When shouldAutorotateToInterfaceOrientation: is called, I would like to grab an opaque view snapshot, a screenshot if you will, of the user view before rotation. Then after the rotation is completed and the system has applied the view transforms etc, I can show the snapshot view I saved and animate a transition of my choice to my new view. After it is completed, I can release my snapshot and move on.

有没有办法做到这一点并不昂贵?

Is there a way to do this that is not expensive?

我能想到的唯一其他选择是在除默认方向之外的所有方向上返回NO,然后通过应用我自己的动画和变换做出反应。我更喜欢使用系统执行此操作,因为我觉得自己可能会在手动旋转视图中导致未定义键盘行为等。

The only other option I can think of is to return NO on all orientations other than my default one, and then react by applying my own animations and transforms. I'd prefer to use the system to do this however, as I feel it is likely doing it myself could cause "undefined" keyboard behavior in the manually rotated view, etc.

想法?

推荐答案

我可以保证你[window drawRect:]不起作用。大多数视图都没有实现drawRect:并且它肯定不会像在Mac上一样递归。

I can guarantee you [window drawRect:] won't work. Most views don't implement drawRect: and it's certainly not recursive like on the mac.

drawInContext:也非常多,因为它从根本上要求图层绘制本身,而不是递归的子层。 默认实现什么都不做。说了这么多。

drawInContext: is also pretty much hosed simalarly, as it's fundamentally asking the layer to draw itself, not its sublayers recursively. "Default implementation does nothing." says it all.

我通过执行以下操作为应用程序商店中的Clif Bar Save Our Snow应用程序实现此功能:

I implementing this for the Clif Bar Save Our Snow app in the app store by doing the following:


  • 递归地遍历图层层次结构,从每个图层获取内容。如果它闻起来像一个图像,使用CGContext方法将其绘制到您的上下文中。

  • 走路时,确保在递归调用时应用每个层的转换/位置

是的,这是一个相当大的痛苦。但是,是的,它运作良好,并通过Apple完成:)。

Yes, a rather large pain in the ass. But yes, it works pretty well and got through by Apple :).

这篇关于如何捕获当前视图截图并在代码中重用? (iPhone SDK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆