iOS 手势与触摸

  1. 响应触摸方法:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesBegan");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesMoved");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesEnded");
}
//当系统事件(如内存不足、电话呼入)终止了触摸事件时响应该方法
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesCancelled");
}
  1. 添加了手势的控件常需要设置的属性:
    (UILabel,UIImageVIew默认不允许用户交互)
//UILabel默认不允许用户交互,故如此设置
_lbtext.userInteractionEnabled = YES;
//设置控件为多点触控
 _lbtext.multipleTouchEnabled = YES;
  1. 手势处理器 UIGestureRecognizer
    UIGestureRecognizer是所有手势控制器的基类,它提供了如下子类:

    • UITapGestureRecognizer:轻拍手势
    • UIPinchGestureRecognizer : 捏合手势
    • UIPanGestureRecognizer :拖拽手势
    • UISwipeGestureRecognizer : 轻扫手势
    • UIRotationGestureRecognizer :旋转手势
    • UILongPressGestureRecognizer :长按手势
  2. UITapGestureRecognizer 轻拍 -常用于 “加在空白处,关闭键盘”

UITapGestureRecognizer *tapGesture2 
= [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(closeKey)];
[self.view addGestureRecognizer:tapGesture2];
-(void)closeKey{
    [self.view endEditing:YES];
}
  1. UISwipeGestureRecognizer 轻扫
    特有属性: dirction:设置轻扫的方向,支持上,下,左,右四个方向。
    (设置四个方向的轻扫)
for (int i=0; i<4; i++) {
    UISwipeGestureRecognizer *swipeGesture 
    = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandle:)];
        [swipeGesture setDirection:1<
  1. 拖动时候肯定有个速度,返回值就是你拖动时X和Y轴上的速度,速度是矢量,有方向。
CGPoint velocity = [(UIPanGestureRecognizer *)gestureRecognizer     velocityInView:self.view];

参考资料:

  1. 知识链接1

  2. 知识链接2

  3. 响应链简析

你可能感兴趣的:(iOS 手势与触摸)