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

touchesBegan: withEvent: <--- with UIScrollView / UIImageView的更多相关文章

  1. UIScrollView方法 属性详解

    --前言:UIScrollView使用非常广,本文研究UIScrollView各属性和方法,明白它们的意义.作用.在后面的一篇文章有整理UIScrollView一些常见用法以及一些效果的实现思路. - ...

  2. iOS Programming Views :Redrawing and UIScrollView

    iOS Programming Views :Redrawing and UIScrollView  1.1 event  You are going to see how views are red ...

  3. iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

    -- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事 ...

  4. iOS-触摸事件、手势识别、摇晃事件、耳机线控

    概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事件(手势操作).运动事件. ...

  5. ios9基础知识(UI)总结

    UIWindow.UILabel.UIColor.UIScreen.UIViewController.UIView.UIControl.UIButton.IBOutlet.IBAction.UISte ...

  6. 转发:iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

    -- iOS事件全面解析 转载来自崔江涛(KenshinCui) 链接:http://www.cnblogs.com/kenshincui/p/3950646.html 概览 iPhone的成功很大一 ...

  7. iPhone与iPad开发实战读书笔记

    iPhone开发一些读书笔记 手机应用分类1.教育工具2.生活工具3.社交应用4.定位工具5.游戏6.报纸和杂志的阅读器7.移动办公应用8.财经工具9.手机购物应用10.风景区相关应用11.旅游相关的 ...

  8. iOS事件传递->处理->响应

    前言: 按照时间顺序,事件的生命周期是这样的: 事件的产生和传递(事件如何从父控件传递到子控件并寻找到最合适的view.寻找最合适的view的底层实现.拦截事件的处理)->找到最合适的view后 ...

  9. NSRunLoop

    1.什么是RunLoop 运行循环 一个线程对应一个RunLoop,主线程的RunLoop默认已经启动,子线程的RunLoop得手动启动(调用run方法) RunLoop只能选择一个Mode启动,如果 ...

随机推荐

  1. 一篇顺手的Ubuntu+caffe配置笔记

    主要参考: https://github.com/lbzhang/dl-setup http://ouxinyu.github.io/Blogs/20151108001.html http://www ...

  2. [转]Jmeter(一)-精简测试脚本

    通过jmeter代理录制脚本后,会产生大量的无用的请求,尽管在代理中已经过滤了一部分图片或者CSS.JS文件. 手动查看主要的请求:这里主要关注登陆请求,要确定有效的URL请求 删除除/Login.a ...

  3. MAMP、wordpress安装

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; font: 12.0px Helvetica } p.p4 { margin: ...

  4. Java的设计模式----strategy(策略模式)

    设计模式: 一个程序员对设计模式的理解: “不懂”为什么要把很简单的东西搞得那么复杂.后来随着软件开发经验的增加才开始明白我所看到的“复杂”恰恰就是设计模式的精髓所在,我所理解的“简单”就是一把钥匙开 ...

  5. php 中文转拼音首字母问题

    <?php /* 中文汉字转拼音首字母的PHP简易实现方法. 要求: 只能是GB2312码表里面中文字符 转换得到字符串对应的拼音首字母大写. 用法: echo zh2py::conv('Chi ...

  6. flume从kafka读取数据到hdfs中的配置

    #source的名字 agent.sources = kafkaSource # channels的名字,建议按照type来命名 agent.channels = memoryChannel # si ...

  7. winsock编程IOCP模型实现代码

    winsock编程IOCP模型实现代码 话不多说,上代码.借鉴<windows核心编程>部分源码和CSDN小猪部分代码. stdafx.h依赖头文件: #include <iostr ...

  8. 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage“提示信息

        在安装过vs2015之后出现未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage“提示信息在VS的安装目录下,找到 ...

  9. scala 数组 基本类型

    变量尽量用valvar 是不可变 final 常用的 Int̵ Double̵ Long̵ String没有基本类型.scala 任何对象都继承Any Int Double 继承AnyVal Stri ...

  10. 【定位:PDF文件定位关键字所在坐标和页码】

    iText简介: iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件 ...