iOS 事件处理之UIResponder简介
2016-06-11 01:50
537 查看
在用户使用app过程中,会产生各种各样的事件
iOS中的事件可以分为3大类型:触摸事件、加速计事件、远程控制事件
在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件。我们称之为“响应者对象”
UIApplication、UIViewController、UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160609105110808-68370753.png)
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160609105152793-739043557.png)
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160609105203511-249775394.png)
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160609105625558-240704279.png)
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160609105935902-333991984.png)
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160609105956199-1665106087.png)
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160611134731886-2128474706.png)
iOS中的事件可以分为3大类型:触摸事件、加速计事件、远程控制事件
在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件。我们称之为“响应者对象”
UIApplication、UIViewController、UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160609105110808-68370753.png)
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160609105152793-739043557.png)
// 当手指开始触摸view // NSArray,字典,NSSet(无序) - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"%ld", touches.count); NSLog(@"%s",__func__); } // 当手指在view上移动的时候 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"%s",__func__); // 获取UITouch对象 UITouch *touch = [touches anyObject]; // 获取当前点 CGPoint curP = [touch locationInView:self]; // 获取上一个点 CGPoint preP = [touch previousLocationInView:self]; // 获取x轴偏移量 CGFloat offsetX = curP.x - preP.x; // 获取y轴偏移量 CGFloat offsetY = curP.y - preP.y; // 修改view的位置(frame,center,transform) self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY); // self.transform = CGAffineTransformMakeTranslation(offsetX, 0); } // 当手指离开这个view的时候 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"%s",__func__); } // 当触摸事件被打断的时候调用(电话打入) - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"%s",__func__); }
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160609105203511-249775394.png)
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160609105625558-240704279.png)
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160609105935902-333991984.png)
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160609105956199-1665106087.png)
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160611134731886-2128474706.png)
![](http://images2015.cnblogs.com/blog/575661/201606/575661-20160609112632730-1666136470.png)
相关文章推荐
- Android-基本UI控件
- vue.js
- UIResponder介绍
- poj 1019 Number Sequence 第i位上的数字 (组合数学)
- 严重: Exception occurred during processing request: null原因解析
- UIBezierPath+CAShapeLayer绘图
- POJ1947 Rebuilding Roads
- ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet
- 快速构建不同UI样式的列表适配器方法-AbsAdapter
- Nutch抓取错误——java.sql.BatchUpdateException: Incorrect string value: '\xF2\xA3\xAC\xB7\xEF\xBF.
- android 进阶之UI
- Leetcode Repeated DNA Sequences
- QT模态对话框用法(在UI文件中设置Widget背景图,这个图是一个带阴影边框的图片——酷)
- POJ 2524 Ubiquitous Religions (简单并查集,三种方式)
- java.util.AbstractStringBuilder源码分析
- QNetworkAccessManager跳转URL处理(使用QNetworkRequest::RedirectionTargetAttribute获得跳转URL)
- 跨平台生成GUID/UUID
- UITabBarController+UINavigationController+UIToolBar
- GUIDE D'IONIC [Français]
- GUI_POINT的坐标