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. 面试题-Java基础-开发环境

    1.什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行的字节码文件.Java被设 ...

  2. java 格式化输出方法

    在javaSE5中推出了printf方法来输出文本到控制台,在java中现在有如下方法可以输出文本: 1.System.out.println(....) //输出并换行 2.System.out.f ...

  3. ng-Directive

    伪代码: var myModule = angular.module(...); myModule.directive('namespaceDirectiveName', function facto ...

  4. 使用webview加载html图片、表单超屏幕问题

    webView加载html代码时,使用webView自带的 scalesPageToFit 可以解决图片所带来的超过屏幕问题:但是,所带来的问题就是文字变小了,怎样让图片边小,并且文字还是原来html ...

  5. Quartz(任务调度)- Cron

    参照:http://www.cnblogs.com/linjiqin/archive/2013/07/08/3178452.html 工具:在线生成Cron 语法规则: Seconds Minutes ...

  6. SPA UI-router

    ------------------------------------------------------------------------------------ SPA SPA(单页面应用): ...

  7. 安装Postgresql踩过的坑

    PG安装相关的 1  系统语言的设置 PG的安装,和系统的locale设置有一定的关系,需要设置,如在.profile或.bashrc中 也可以运行下面的命令: dpkg-reconfigure lo ...

  8. 顺序栈和链式栈(C++实现)

    顺序栈,是一种基于数组的存储表示. 实现类代码如下: template<class T> class SeqStack{ T *element; int top; int maxSize; ...

  9. Hadoop-2.6.0安装文档

    前段时间在dataguru上报了一个hadoop的培训班,希望能够帮助自己更快的了解.掌握并且熟悉hadoop的开发和原理. 上一期的作业是要自己搭建一个hadoop的环境,并能运行mapreduce ...

  10. Windows下NexusPHP搭建PT站过程

    搭建环境:Win7+XAMPP(5.5) 网站根目录: D:\IT\XAMPP5.5\htdocs\ 1 下载源码程序http://sourceforge.net/projects/nexusphp/ ...