1. 最常用的textView取消第一響應者:
[textView resignFirstResponder];
2.ios 7以后系統(tǒng)短信方式,退出鍵盤,可拉可壓,只要是scrollView的子類都可以實現(xiàn)
scrollView.keyboardDismissMode=UIScrollViewKeyboardDismissModeInteractive;
3.自定義滑到某個位置退出鍵盤,一般是在系統(tǒng)鍵盤上添加了工具欄,導致不能使用keyboardDismissMode方式的時候。
CGPointtouchPoint = [self.sTextView.panGestureRecognizerlocationInView:self.sTextView];
touchPoint = [self.sTextViewconvertPoint:touchPointtoView:self.sTextView.superview];
(touchPoint.x,touchPoint.y)就是手指所在屏幕的滑動時的位置
通過scrollView的代理方法:
- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
可以得到手指觸摸的初始位置,然后根據(jù)滑動時的位置,就可以判斷出什么時候可以收回鍵盤。
4.退出鍵盤時實現(xiàn)手指滑到哪里,鍵盤位置到哪里,就是可以隨意改變鍵盤的高度,不讓它一下子就消失,只要拿到鍵盤對象就可以輕松搞定了。在鍵盤的監(jiān)聽事件里面實現(xiàn):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.獲得手勢位置,如果控件自身可以滑動,這幾個代理方法就不會執(zhí)行,touch事件也不會執(zhí)行
- (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];