iOS开发之手势gesture详解(二)
与其他用户界面控件交互
UIControl子类会覆盖parentView的gesture.例如当用户点击UIButton时,UIButton会接受触摸事件,它的parentView不会接收到.这仅适用于手势识别重叠的默认动作的控制,其中包括:
- 一根手指单击动作:UIButton, UISwitch, UIStepper, UISegmentedControl, and UIPageControl.
- 一根手指擦碰动作:UISlider
- 一根手指拖动动作:UISwitch
包含多点触摸的事件
在iOS中,touch是指一根手指在屏幕上滑动、移动.而手势是指有一根或多根手指,它的类是UITouch对象.多点触摸事件是UIEventTypeTouches
当发生触摸时,会触发下列方法:
- touchesBegan:withEvent :触摸屏幕时发生
- touchesMoved:withEvent :手指在屏幕上移动时发生
- touchesEnded:withEvent :触摸结束时发生,即手指离开屏幕时
- touchesCancelled:withEvent :触摸被系统取消时发生,如有电话时,该触摸会被取消
改变默认的事件传递顺序
你可以通过改变UIGestureRecognizer子类的属性来改变默认的事件传递
- @property(nonatomic) BOOL delaysTouchesBegan; // default is NO. causes all touch events to be delivered to the target view only after this gesture has failed recognition. set to YES to prevent views from processing any touches that may be recognized as part of this gesture
- @property(nonatomic) BOOL delaysTouchesEnded; // default is YES. causes touchesEnded events to be delivered to the target view only after this gesture has failed recognition. this ensures that a touch that is part of the gesture can be cancelled if the gesture is recognized
如果一个gesture recognizer检测到了一个不属于它的touch,它将手势直接发送给它的view,此时会调用ignoreTouch:forEvent: 方法直接传递给view.
创建自定义gesture recognizer步骤
- 创建UIGestureRecognizer的子类
- #import <UIKit/UIGestureRecognizerSubclass.h>
- 在子类冲重载下列方法
1.- (void)reset;
2.- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
3.- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
4.- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
5.- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;