Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

ios - Animate the keyboard in sync with the UIView while edge-swiping back in iOS7

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.)

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.

Desired behavior:

Desired behavior

Current behavior:

Keyboard tearing

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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];

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...