美文网首页专注iOS开发iOS精选移动开发
UIScrollView点击空白处退出键盘方法(含与键盘手写冲突

UIScrollView点击空白处退出键盘方法(含与键盘手写冲突

作者: vincent涵 | 来源:发表于2016-05-30 14:56 被阅读1878次

UIScrollView 上如果有UITextField的话,结束编辑(退出键盘)直接用touchesBegan方法无效,需要再给UIScrollView加一个分类,重写几个方法。
网上已经有很多前辈给了相关代码是这样的(阅前提示:这样是有问题的!):

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];
    
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesMoved:touches withEvent:event];
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesEnded:touches withEvent:event];
    [super touchesEnded:touches withEvent:event];
}

这样会有一个严重问题,就是使用手写输入法输入中文会导致崩溃(虽然使用手写输入法的人不多,但也不能无视他们)。被坑死,问题是百度出来尼玛80~90%全是这种解决方法。坑死人!

有一些前辈对于“UIScrollView点击空白处退出键盘”就提出了另一种解决方法:加一层view,给view一个点击事件,退出键盘。

但是我的项目中已经被前一种方法坑了,已经有用户反映手写崩溃,换第二种方法的话很麻烦,需要修改之后重新提交审核,不能及时解决,我需要及时的用JSPatch线上打补丁解决。调试了很久,我发现手写键盘在调用UIScrollView的这个分类的方法时,self的类型是UIKBCandidateCollectionView,一种系统没有暴露出来的类型,应该是UIScrollView的一个子类,所以解决办法就呼之欲出了,直接上代码。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self isMemberOfClass:[UIScrollView class]]) {
        
    } else {
        [[self nextResponder] touchesBegan:touches withEvent:event];
        if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
            [super touchesBegan:touches withEvent:event];
        }
    }
    
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self isMemberOfClass:[UIScrollView class]]) {
        
    } else {
        [[self nextResponder] touchesMoved:touches withEvent:event];
        if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
            [super touchesMoved:touches withEvent:event];
        }
    }
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self isMemberOfClass:[UIScrollView class]]) {
        
    } else {
        [[self nextResponder] touchesEnded:touches withEvent:event];
        if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
            [super touchesEnded:touches withEvent:event];
        }
    }
}

手写输入法崩溃完美解决O(∩_∩)O~~

相关文章

网友评论

  • 时光浅影:我的是用了别人使用的类别导致的,把那个类别去了就没有崩溃了
  • 游龙飞雪:多谢大侠!!!
  • yljbyj:LZ, ios 11点击 textField 程序崩溃,错误信息是:-[UIKBCandidateCollectionView interactiveMode]: unrecognized selector sent to instance 0x7fd88d107600,知道是什么原因吗??
  • 霖溦:UIScrollViewKeyboardDismissModeOnDrag这个系统的方式可以解决滑动回收键盘的问题,但是点击不可以。。。
    霖溦:还是感谢楼主的细心研究和分享
  • 0271fb6f797c:- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView方法中调用[self.View endEditing:YES];当键盘为手写键盘时一直崩溃,百度到的方法都不太好,有解决办法吗?
  • 向东七里:但是输入后删除依然会崩溃啊,楼主解决了么?
    vincent涵:我没有遇到输入后删除会崩溃的情况,不太清楚具体原因,推荐你了解使用一下IQKeyboardManager这个库,很方便,现在已经不自己手写去控制键盘了:yum:
  • 向东七里:谢谢🙏,这种干货太紧急了,完美解决问题。。。。
  • 会飞不一定是超人:刚也遇到相同的问题,感谢楼主
  • Jeremy_Mi:多谢,被这个问题困扰好久了,被百度到的东西各种坑死.....
    vincent涵:@黑白小熊猫 解决就好:blush:
  • 挠叔:3q 解决了我的问题 。喜欢这种分析加实测的文章,而不是网上那些一大堆复制粘贴的
    vincent涵:@挠叔 :smile:
  • 023e0dd0b2ef:3q 解决了我的问题 。喜欢这种分析加实测的文章,而不是网上那些一大堆复制粘贴的
    vincent涵:谢谢支持:yum:

本文标题:UIScrollView点击空白处退出键盘方法(含与键盘手写冲突

本文链接:https://www.haomeiwen.com/subject/vrxcdttx.html