touchesBegan: withEvent: / touchesMoved: withEvent: / touchesEnded: withEvent: 等只能被UIView捕获(如有问题请指出对请指出,路过的大牛请勿喷),当我们创建

UIScrollView 或 UIImageView 时,当点击时UIScrollView 或 UIImageView 会截获touch事件,导致touchesBegan: withEvent:/touchesMoved: withEvent:/touchesEnded: withEvent: 等方法不执行。解决办法:当UIScrollView 或 UIImageView 截获touch事件后,让其传递下去即可(就是传递给其父视图UIView)

解决方法一:

可以通过写UIScrollView 或 UIImageView 的category 重写touchesBegan: withEvent: / touchesMoved: withEvent: / touchesEnded: withEvent: 等来实现

解决方法二: 覆盖一个全屏View

并在- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{} 做相关处理

  1. //
  2. //  UIScrollView+UITouch.m
  3. //
  4. //
  5. //  Created by MLS on 15/11/20.
  6. //  Copyright © 2015年 mls. All rights reserved.
  7. //
  8. #import "UIScrollView+UITouch.h"
  9. @implementation UIScrollView (UITouch)
  10. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  11. {
  12. // 选其一即可
  13. [super touchesBegan:touches withEvent:event];
  14. //    [[self nextResponder] touchesBegan:touches withEvent:event];
  15. }
  16. @end

解决方法三:

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~~

 

参考链接:

1.http://blog.csdn.net/maolianshuai/article/details/49949751

2.http://www.jianshu.com/p/0e9cb4a8c3a0

3.http://www.jianshu.com/p/0e9cb4a8c3a0

最新文章

  1. php变量-单引号不编译,双引号编译
  2. ASP.NET MVC4 Forms 登录验证
  3. iis日志查看
  4. Excel应该这么玩——2、命名列:消除地址引用
  5. Microsoft-pubs(图书馆管理系统)-数据库设计
  6. Linux下的管道命令有这些:
  7. 【DataStructure】Some useful methods about linkedList(二)
  8. Android 简单的代码混淆
  9. CODE[VS]-蛇形矩阵-模拟-天梯白银
  10. 异常:Unknown lifecycle phase &quot;mvn&quot;. You must specify a valid lifecycle
  11. chrome_options
  12. 芝麻HTTP:Python爬虫实战之抓取爱问知识人问题并保存至数据库
  13. shell脚本头,#!/bin/sh与#!/bin/bash的区别.
  14. EF ORM
  15. 【译】第七篇 SQL Server安全跨数据库所有权链接
  16. MySQL关于根据日期查询数据的sql语句
  17. js中如何把字符串转化为对象、数组示例代码
  18. Winfrom控件使用
  19. Spring IOC(二)beanName 别名管理
  20. XHR工厂的实现

热门文章

  1. 1、&lt;img /&gt;标签
  2. iOS跳转系统设置界面
  3. ubuntu 14.04—解决软件中心进度条卡死的问题
  4. 扯蛋css
  5. hadoop操作
  6. group by 和count 联合使用问题
  7. QTP脚本汇总比较有价值
  8. centos7,yum安装的redis用systemctl无法启动
  9. Java代码之输出参数和(强制类型转换)
  10. Hadoop实践