触摸、手势与响应者链条

1>4个触摸事件【触摸事件主要是针对视图】

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

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

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

 2. pointInside 方法

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

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

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

判断触摸点是否在视图内部,在视图内部返回 YES ,否则返回 NO.

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

提示:以上两个方法会被循环递归,多次调用!直至找到最适合响应用户请求的视图!所以,一般情况下,不要轻易重写这两个方法,因为一旦方法内部使用了消耗量大的代码,会严重影响系统的性能!

// 重写pointInside方法,扩大按钮的点击范围
@interface ExtendedHitButton: UIButton

+ (instancetype) extendedHitButton;

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

@end


@implementation ExtendedHitButton

+ (instancetype) extendedHitButton
{
    return (ExtendedHitButton *) [ExtendedHitButton buttonWithType:UIButtonTypeCustom];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    CGRect relativeFrame = self.bounds;
    UIEdgeInsets hitTestEdgeInsets = UIEdgeInsetsMake(-35, -35, -35, -35);
    CGRect hitFrame = UIEdgeInsetsInsetRect(relativeFrame, hitTestEdgeInsets);
    return CGRectContainsPoint(hitFrame, point);
}

@end
View Code
原文地址:https://www.cnblogs.com/yaoxc/p/3980058.html