在 iOS7 中边缘滑动时,使键盘与 UIView 同步动画

Animate the keyboard in sync with the UIView while edge-swiping back in iOS7(在 iOS7 中边缘滑动时,使键盘与 UIView 同步动画)
本文介绍了在 iOS7 中边缘滑动时,使键盘与 UIView 同步动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我希望在 iOS7 中获得类似于 Messages 应用程序(在大多数短信应用程序中也很常见)的行为,其中在对话视图中从屏幕左边缘向右滑动的行为类似于 UINavigationController 中的后退按钮.

I'd like to get the behavior similar to Messages app (also common in most texting apps) in iOS7, where in a conversation view swiping right from the left edge of the screen would behave like the back button in a UINavigationController.

我已经设法实现了这种行为,但是,如果键盘在当前视图中打开,当我开始向后滑动时,键盘会卡住并且在我移动手指时不会随着视图向右移动.我想将键盘和呈现视图作为一个单元进行动画处理,而不是好像键盘位于其他视图之上并且它们在其后面进行动画处理,这就是我现在得到的(参见第二个屏幕截图):

I have managed to implement this behavior, however, if the keyboard is open in the presenting view, when I start swiping back, the keyboard gets stuck and does not animate with the view to the right as I move my finger. I'd like to animate keyboard and the presenting view as one unit, not as if keyboard is on top of the other views and they are animating behind it, which is what I get now (see the second screenshot):

(更新:请注意,键盘最终会在主视图动画完成后消失;我关注的是键盘滑动过程中的位置, 并且当您在中途继续触摸设备时,与实际视图不同步.第二个屏幕截图阐明了这种期望的行为.我也想知道为什么它不是默认设置.)

(UPDATE: Note that the keyboard will eventually go away after the main view animation is finished; what I am focused on is the position of keyboard during the swipe process, and when you keep touching the device half of the way, which is not in sync with the actual view. The second screenshot clarifies this desired behavior. I also wonder why it is not the default.)

通过简单地在 Xcode 5.0.2 中创建一个新的主从 iPhone 应用程序并在 StoryBoard 的详细视图(最好在上半部分的某个位置)添加一个文本字段,运行该应用程序,很容易复制该问题,添加一个项目,点击它进入详细视图,然后单击您添加的文本字段.从设备左侧边缘滑动,同时将手指放在设备上,您会看到问题.

It is easy to replicate the issue by simply creating a new master-detail iPhone app in Xcode 5.0.2 and adding a Text Field to the detail view (preferably somewhere in the upper half) in the StoryBoard, running the app, adding an item, tapping on it to go to the detail view and clicking on the text field you added. Edge-swipe from the left side of the device while keeping your finger on it and you'll see the issue.

期望的行为:

当前行为:

推荐答案

不幸的是,没有内置的方法可以做到这一点.我真的希望 UIViewControllers 会有类似 UIScrollViewKeyboardDismissModeInteractive 的东西.

Unfortunately, there is no built-in method to do that. I really hope there will be something like UIScrollViewKeyboardDismissModeInteractive for UIViewControllers.

目前,要在 viewController 之间制作任何动画,您应该使用 transitionCoordinator:

For now, to do any animations in-between viewControllers, you should use a transitionCoordinator:

- (BOOL)animateAlongsideTransition:(void (^)(id <UIViewControllerTransitionCoordinatorContext>context))animation
                        completion:(void (^)(id <UIViewControllerTransitionCoordinatorContext>context))completion;

- (BOOL)animateAlongsideTransitionInView:(UIView *)view
                               animation:(void (^)(id <UIViewControllerTransitionCoordinatorContext>context))animation
                              completion:(void (^)(id <UIViewControllerTransitionCoordinatorContext>context))completion;

对于键盘,你应该这样做:

For the keyboard you should do something like this:

[self.transitionCoordinator animateAlongsideTransitionInView:self.keyboardSuperview
                                                   animation:
^(id<UIViewControllerTransitionCoordinatorContext> context) {
    self.keyboardSuperview.x = self.view.width;
}
                                                  completion:nil];

至于 keyboardSuperview - 你可以通过创建一个假的 inputAccessoryView 来获得它:

As for keyboardSuperview - you can get that by creating a fake inputAccessoryView:

self.textField.inputAccessoryView = [[UIView alloc] init];

那么superview就是self.textField.inputAccessoryView.superview

Then the superview will be self.textField.inputAccessoryView.superview

这篇关于在 iOS7 中边缘滑动时,使键盘与 UIView 同步动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to resize UITextView on iOS when a keyboard appears?(出现键盘时如何在iOS上调整UITextView的大小?)
How to reliably detect if an external keyboard is connected on iOS 9?(如何可靠地检测 iOS 9 上是否连接了外部键盘?)
How to mimic Keyboard animation on iOS 7 to add quot;Donequot; button to numeric keyboard?(如何在 iOS 7 上模拟键盘动画以添加“完成数字键盘的按钮?)
How do I dismiss the iOS keyboard?(如何关闭 iOS 键盘?)
Is possible to simulate touch event using an external keyboard on ios jailbroken?(是否可以在 ios 越狱后使用外部键盘模拟触摸事件?)
How do I retrieve keystrokes from a custom keyboard on an iOS app?(如何从 iOS 应用程序上的自定义键盘检索击键?)