如何手动旋转键盘(带动画)? [英] How can I manually rotate the keyboard (With animation)?

查看:193
本文介绍了如何手动旋转键盘(带动画)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个管理UITextView的控制器。通常,在支持多个接口方向的应用程序中,我将简单地覆盖 -shouldAutorotateToInterfaceOrientation:,设置适当的自动调整大小的掩码,并沿着我的快乐方式。当我沿着我的快乐的方式,如果我开始编辑这个UITextField在,例如,肖像模式,然后旋转设备到横向,键盘将动画很好地横向模式以及视图的其余部分。这是我想要的。

I have a controller that manages a UITextView. Normally, in an app that supports multiple interface orientations, I would simply override -shouldAutorotateToInterfaceOrientation:, set the appropriate auto-resizing masks, and go along my merry way. While going along my merry way, if I were to begin editing this UITextField in, say, portrait mode, then rotate the device to a landscape orientation, the keyboard would animate nicely to landscape mode along with the rest of the view. This is what I want.

现在,在这个特定的应用程序中,我有一个根视图,只有以纵向模式。这是相机预览视图。我也有一个重叠视图的相机视图,我想支持所有的界面方向。这是UITextView所在的位置。

Now, in this particular app, I have a root view that must only ever be in portrait mode. This is a camera preview view. I also have an overlay-view for the camera-view that I would like to have support all interface orientations. This is where the UITextView is located.

所以,作为固定方向根视图的结果,我不能使用内置旋转:我必须手动执行。这不是一个问题,我可以注册 UIDeviceOrientationDidChangeNotification ,并手动为叠加视图动画适当的旋转和框架调整。

So, as a result of the fixed-orientation root view, I cannot use built in rotation: I have to do this manually. This isn't a problem, I can register for the UIDeviceOrientationDidChangeNotification and manually animate the appropriate rotation and frame adjustment for the overlay view.

这样做足以使视图正确定位,但键盘仍然只会以纵向显示。再次,我可以使用UIApplication的 -setStatusBarOrientation:来更正这一点。每次我收到设备已旋转的通知。

That's good enough to get the view positioned correctly, but the keyboard would still only ever show in portrait orientation. Again, I can correct this with UIApplication's -setStatusBarOrientation: every time I get a notification that the device has rotated.

但这就是问题出现的地方。我希望结合:

But that's where the problem arises. I would hope that the combination of:


  • 注册轮播通知

  • 手动旋转视图

  • 设置状态栏方向(带动画)

足以完全复制自动轮播行为。但是它在一个重要的方面缺乏:如果键盘被显示在一个方向,然后旋转到另一个方向,键盘将保持原始位置,如 -setStatusBarOrientation:从来没有调用过。据我所知,影响键盘演示的唯一的事情是应用程序的状态栏方向,并且只有一种方法来设置它。所以我失去了运气吗?是否真的不可能像手动旋转一样用手动旋转键盘?

would be enough to exactly replicate the automatic rotation behavior. But it falls short in one important way: If the keyboard were to be shown in one orientation, then rotated to another, the keyboard would remain defiantly in the original position, as if -setStatusBarOrientation: were never called. As far as I know, the only thing that affects the keyboard's presentation is the application's status bar orientation, and there is only the one method for setting it. So am I out of luck? Is it really impossible to make the keyboard rotate with a view manually like it does with auto-rotation?

附录

为了避免覆盖旧地面,我尝试了以下解决方案 hacks:


  • 改变,辞职,并立即成为第一响应者。这使得键盘移动到适当的方向,但它没有动画,偶尔会做真的很糟糕的事情,如显示风景大小键盘的肖像,反之亦然。

  • 方向变化只是辞职第一反应者。这也有奇怪的行为,即时移动(但不是调整大小)键盘到新的方向,然后用动画解雇它。

推荐答案

我们的一个应用程序执行此操作(因为我们没有'当使用原始设计时,使用疯狂的定位堆栈代码,说不!我记得通过在键盘启动(ickyyyy)时禁用方向来解决此问题。

One of our apps does this (because we didn't say "no!" loudly enough when presented with the original design) using crazy "orientation stack" code. I remember fixing this problem by disabling orientation when the keyboard is up (ickyyyy).

而是考虑通过反向旋转相机预览视图支持旋转,而不是强制VC到仅纵向。如果这看起来有点奇怪,你可以做讨厌的黑客版本:添加相机预览视图作为窗口的子视图,在你的VC视图下,并给你的VC的视图一个透明的背景。

Instead, consider supporting rotation by "counter-rotating" the camera preview view instead of forcing the VC to portrait-only. If this looks a bit odd, you can do the "nasty hack" version: Add the camera preview view as a subview of the window, under your VC's view, and give your VC's view a transparent background.

这篇关于如何手动旋转键盘(带动画)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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