ios开发之触摸&手势识别

概要: 4个触摸事件、6个手势识别、响应者链条

1、4个触摸事件

1> 触摸事件主要是针对视图的,包括

- (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;

 

2、6个手势识别

–UITapGestureRecognizer(点按)
–UIPinchGestureRecognizer(捏合)
–UIPanGestureRecognizer(拖动)
–UISwipeGestureRecognizer(轻扫)
–UIRotationGestureRecognizer(旋转)
–UILongPressGestureRecognizer(长按)

3、响应者链条

1. hitTest方法,用于检测具体响应用户触摸点视图的方法

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

   

    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;

    在视图内部返回YES,否则返回NO

    联合使用来判断用户触摸点的位置是否在指定的视图内部,如果是,表示该视图可以接收用户交互。

    以上两个方法会被循环递归,多次调用!直至找到最适合响应用户请求的视图!

    提示:一般情况下,不要轻易重写这两个方法,因为一旦方法内部使用了消耗量大的代码,会严重影响系统的性能!

    窍门:一旦出现示例中需要拦截触摸事件的情况,可以与美工或者策划人员沟通,调整界面!

    1) 如果返回nil,表示没有响应的视图

    2) 如果返回视图,表示该视图接收用户的响应

 2. 接收用户触摸响应的几个条件

    1) self.userInteractionEnabled = YES;   允许接收用户响应

    2) self.hidden = NO;                    只有现实的视图才能接收用户触摸

    3) self.alpha > 0.01;                   视图的透明度一定要可见

    提示:并不是所有的控件都默认接收用户交互的,譬如:UIImageViewUILabel

 3. 参数说明

    point   用户触摸的点,相对于当前视图坐标系的坐标点

    event   用户触摸事件,开发中一般程序员不使用,该事件用于在响应者链条上传递

原文地址:https://www.cnblogs.com/liufeng24/p/3500653.html