iOS中的事件,手势识别
2015-12-26 18:39
330 查看
iOS中的事件可以分为三大类型:1.触摸事件触摸事件的传递是从父控件到子控件,如果父控件不能接收触摸事件,那么子控件就不可能接收到触摸事件不能接收触摸事件的几种情况:1.userInteractionEnabled = NO; 2.hidden = yes; 3.alpha = 0.0--0.01//touches中存放的是UITouch对象
//触摸事件 - (void)touchBegin:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchMoved:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchEnded:(NSSet *)touches withEvent:(UIEvent *)event
//比如有电话呼入,会导致触摸事件停止,这时候会调用该方法 - (void)touchCancelled:(NSSet *)touched withEvent:(UIEvent *)event
<pre class="objc" name="code">//一次完整的触摸过程,会经历3个状态://触摸开始:- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event//触摸移动:- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event//触摸结束:- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event//触摸取消(可能会经历):- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event//4个触摸事件处理方法中,都有NSSet *touches和UIEvent *event两个参数//一次完整的触摸过程中,只会产生一个事件对象,4个触摸方法都是同一个event参数//如果两根手指同时触摸一个view,那么view只会调用一次touchesBegan:withEvent:方法,touches参数中装着2个UITouch对象//如果这两根手指一前一后分开触摸同一个view,那么view会分别调用2次touchesBegan:withEvent:方法,并且每次调用时的touches参数中只包含一个UITouch对象//根据touches中UITouch的个数可以判断出是单点触摸还是多点触摸2.加速计事件
//加速剂事件- (void)motionBegin:(UIEventSubtype)motion withEvent:(UIEvent *)event- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;3.远程控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;只有继承了UIResponder的对象才可以接受并处理事件,又叫“响应者对象”UITouch 的一些属性widow 触摸产生所在的窗口view 触摸产生所在的视图tapCount 短时间内点按屏幕的次数timestamp 记录了触摸产生或者变化的时间phase 触摸事件所处的状态
UITouchPhase是一个枚举类型,包含:UITouchPhaseBegan(触摸开始)UITouchPhaseMoved(接触点移动)UITouchPhaseStationary(接触点无移动)UITouchPhaseEnded(触摸结束)UITouchPhaseCancelled(触摸取消)
- (CGPoint)locationInView:(UIView *)view;//返回值表示触摸在view上的位置//这里返回的位置是针对view的坐标系的(以view的左上角为原点(0, 0))//调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置- (CGPoint)previousLocationInView:(UIView *)view;//该方法记录了前一个触摸点的位置
UIEvent
没产生一个事件,就是一个UIEvent对象,记录了事件产生的时间和类型
<pre class="objc" name="code">//常见属性//事件类型@property(nonatomic,readonly) UIEventType type;@property(nonatomic,readonly) UIEventSubtype subtype;/*
typedef NS_ENUM(NSInteger, UIEventType) {UIEventTypeTouches,//触摸事件UIEventTypeMotion,//加速计事件UIEventTypeRemoteControl,//远程事件};typedef NS_ENUM(NSInteger, UIEventSubtype) {// available in iPhone OS 3.0UIEventSubtypeNone = 0,// for UIEventTypeMotion, available in iPhone OS 3.0UIEventSubtypeMotionShake = 1,// for UIEventTypeRemoteControl, available in iOS 4.0UIEventSubtypeRemoteControlPlay = 100,UIEventSubtypeRemoteControlPause = 101,UIEventSubtypeRemoteControlStop = 102,UIEventSubtypeRemoteControlTogglePlayPause = 103,UIEventSubtypeRemoteControlNextTrack = 104,UIEventSubtypeRemoteControlPreviousTrack = 105,UIEventSubtypeRemoteControlBeginSeekingBackward = 106,UIEventSubtypeRemoteControlEndSeekingBackward = 107,UIEventSubtypeRemoteControlBeginSeekingForward = 108,UIEventSubtypeRemoteControlEndSeekingForward = 109,};*///事件产生的时间@property(nonatomic,readonly) NSTimeInterval timestamp;
//手势识别
<pre class="objc" name="code">利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势UITapGestureRecognizer(敲击)UIPinchGestureRecognizer(捏合,用于缩放)UIPanGestureRecognizer(拖拽)UISwipeGestureRecognizer(轻扫)UIRotationGestureRecognizer(旋转)UILongPressGestureRecognizer(长按)使用:
//创建手势识别器对象UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];//设置手势识别器对象的具体属性// 连续敲击2次tap.numberOfTapsRequired = 2;// 需要2根手指一起敲击tap.numberOfTouchesRequired = 2;//添加手势识别器到对应的view上[self.iconView addGestureRecognizer:tap];//监听手势的触发[tap addTarget:self action:@selector(tapIconView:)];
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {// 没有触摸事件发生,所有手势识别的默认状态UIGestureRecognizerStatePossible,// 一个手势已经开始但尚未改变或者完成时UIGestureRecognizerStateBegan,// 手势状态改变UIGestureRecognizerStateChanged,// 手势完成UIGestureRecognizerStateEnded,// 手势取消,恢复至Possible状态UIGestureRecognizerStateCancelled,// 手势失败,恢复至Possible状态UIGestureRecognizerStateFailed,// 识别到手势识别UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded};
相关文章推荐
- iOS支付宝支付--密钥制作
- iOS开发之Git的命令以及xocde打不开的原因
- 工作记录8:iOS 传值问题总结(7种传值完美介绍)
- iOS 各种传值方式
- ios中两个viewcontroller之间如何传递数据
- 如何集成inmobi到ios
- iOS中的手势详解
- [IOS开发教程] iOS获取设备信息
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS Label的图文混排
- [IOS开发教程] NSfileManager的使用方法
- iOS开发怎么在Button的显示倒计时
- iOS支付宝支付
- iOS中判断数组长度问题
- iOS开发- 隐藏状态栏(电池栏)
- iOS数据类型
- xcode安装模拟器失败 (could not download and install iOS 7.1 simulator ,需要授权才能安装这些软件包)
- iOS初体验---通知
- iOS- tableview的本地性能优化
- iOS_第3方键盘管理库_IQKeyboardManager