1. 最常用的textView取消第一响应者:
[textView resignFirstResponder];
2.ios 7以后系统短信方式,退出键盘,可拉可压,只要是scrollView的子类都可以实现
scrollView.keyboardDismissMode=UIScrollViewKeyboardDismissModeInteractive;
3.自定义滑到某个位置退出键盘,一般是在系统键盘上添加了工具栏,导致不能使用keyboardDismissMode方式的时候。
CGPointtouchPoint = [self.sTextView.panGestureRecognizerlocationInView:self.sTextView];
touchPoint = [self.sTextViewconvertPoint:touchPointtoView:self.sTextView.superview];
(touchPoint.x,touchPoint.y)就是手指所在屏幕的滑动时的位置
通过scrollView的代理方法:
- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
可以得到手指触摸的初始位置,然后根据滑动时的位置,就可以判断出什么时候可以收回键盘。
4.退出键盘时实现手指滑到哪里,键盘位置到哪里,就是可以随意改变键盘的高度,不让它一下子就消失,只要拿到键盘对象就可以轻松搞定了。在键盘的监听事件里面实现:keyboard是一个
UIView
- (void)keyboardDidShow:(NSNotification*)notification {
if(keyboard)
return;
UIWindow* tempWindow = [[[UIApplicationsharedApplication]windows]objectAtIndex:1];
for(inti =0; i < [tempWindow.subviewscount]; i++) {
UIView*possibleKeyboard = [tempWindow.subviewsobjectAtIndex:i];
if([possibleKeyboardisKindOfClass:NSClassFromString(@"UIPeripheralHostView")]) {
keyboard= possibleKeyboard;
return;
}
}
}
然后可以加个手势轻松搞定
- (void)panGesture:(UIPanGestureRecognizer*)gestureRecognizer {
CGPointlocation = [gestureRecognizerlocationInView:[selfview]];
CGPointvelocity = [gestureRecognizervelocityInView:self.view];
if(gestureRecognizer.state==UIGestureRecognizerStateBegan) {
originalKeyboardY=keyboard.frame.origin.y;
}
if(gestureRecognizer.state==UIGestureRecognizerStateEnded) {
if(velocity.y>0) {
[selfanimateKeyboardOffscreen];
}else{
[selfanimateKeyboardReturnToOriginalPosition];
}
return;
}
CGFloatspaceAboveKeyboard =CGRectGetHeight(self.view.bounds) - (CGRectGetHeight(keyboard.frame) +CGRectGetHeight(textField.frame)) +kFingerGrabHandleHeight;
if(location.y< spaceAboveKeyboard) {
return;
}
CGRectnewFrame =keyboard.frame;
CGFloatnewY =originalKeyboardY+ (location.y- spaceAboveKeyboard);
newY =MAX(newY,originalKeyboardY);
newFrame.origin.y= newY;
[keyboardsetFrame: newFrame];
}
5.获得手势位置,如果控件自身可以滑动,这几个代理方法就不会执行,touch事件也不会执行
- (void)pan:(UIPanGestureRecognizer*)gesture {
NSLog(@"%f", [gesturelocationInView:self].x);
}
- (void)pan:(UIPanGestureRecognizer*)gesture {
NSLog(@"%f", [gesturelocationInView:self.superview].x);
}
- (void)pan:(UIPanGestureRecognizer*)gesture {
NSLog(@"%f", [gesturelocationInView:self.window].x);
}
这个时候你就需要用到这个方法:
CGPointtouchPoint = [self.sTextView.panGestureRecognizerlocationInView:self.sTextView];
touchPoint = [self.sTextViewconvertPoint:touchPointtoView:self.sTextView.superview];