手势

来源:互联网 发布:阿里云域名解析 速度 编辑:程序博客网 时间:2024/04/29 18:27
 //当子视图超出自己的frame时,是否剪切子视图
     self.clipsToBounds = YES;
       
  //是否开启多点触控
self.multipleTouchEnabled = YES;


//拿到在屏幕上点击手指的个数(注意:开启多点触控)
 NSInteger count = touches.count;


 //拿到点击次数
NSInteger tapCount = touch.tapCount;


 //拿到点击的View
 UIView *touchView = touch.view;


 //下一响应者
 UIResponder *next = self.nextResponder;


//拿到当前在view上的坐标
CGPoint nowLocation = [touch locationInView:self];


 //拿到上一次在view上的坐标
 CGPoint lastLocation = [touch previousLocationInView:self];


//通过响应者链寻找控制器
- (UIViewController *)viewController
{
 UIResponder *next = self.nextResponder;
   
    do {
//判断响应者是否是视图控制器
      if ([next isKindOfClass:[UIViewController class]]) {
  return (UIViewController *)next;
        }
 //如果没有找到控制器,那么继续往下一层响应者去找
  next = next.nextResponder;
    } while (next);
   
    return nil;
}


/*---------------点击手势-------------*/
   
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1Action:)];
    //注意:一个点击手势,只能识别一种手势,单击和双击是不同的两个手势
    //设置点击的次数
    tap1.numberOfTapsRequired = 1;
    //设置点击的手指的个数
    tap1.numberOfTouchesRequired = 1;
    //往gestureView上添加一个手势
    [gestureView addGestureRecognizer:tap1];
   
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap2Action:)];
    tap2.numberOfTapsRequired = 2;
    [gestureView addGestureRecognizer:tap2];
   
    //如果参数中的手势出发了,则自身失效
    [tap1 requireGestureRecognizerToFail:tap2];
   
   
    /*---------------轻扫手势-------------*/
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    // 设置方向---向上轻扫
    swipeGesture.direction = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap2Action:)];
    tap2.numberOfTapsRequired = 2;
    [gestureView addGestureRecognizer:tap2];
   
    //如果参数中的手势出发了,则自身失效
    [tap1 requireGestureRecognizerToFail:tap2];
   
   
    /*---------------轻扫手势-------------*/
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    // 设置方向---向上轻扫
    swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
    [gestureView addGestureRecognizer:swipeGesture];
   
   
    /*---------------平移手势-------------*/
    // 平移手势(滑动)
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [gestureView addGestureRecognizer:panGesture];
  
    /*---------------旋转手势-------------*/
    // 旋转手势
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] UISwipeGestureRecognizerDirectionUp;
    [gestureView addGestureRecognizer:swipeGesture];
   
   
    /*---------------平移手势-------------*/
    // 平移手势(滑动)
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [gestureView addGestureRecognizer:panGesture];
  
    /*---------------旋转手势-------------*/
    // 旋转手势
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    [gestureView addGestureRecognizer:rotationGesture];
   
    /*----------捏合手势--------------*/
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [gestureView addGestureRecognizer:pinch];
}


//平移
- (void)panAction:(UIPanGestureRecognizer *)pan
{
    CGPoint p = [pan locationInView:pan.view];
    NSLog(@"%@", NSStringFromCGPoint(p));
}


//长按
- (void)longPress:(UILongPressGestureRecognizer initWithTarget:self action:@selector(rotation:)];
    [gestureView addGestureRecognizer:rotationGesture];
   
    /*----------捏合手势--------------*/
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [gestureView addGestureRecognizer:pinch];
}


//平移
- (void)panAction:(UIPanGestureRecognizer *)pan
{
    CGPoint p = [pan locationInView:pan.view];
    NSLog(@"%@", NSStringFromCGPoint(p));
}


//长按
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"开始长按");
    }else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"结束长按");
    }
}


//旋转
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
    //获取旋转角度---弧度
    CGFloat r = rotation.rotation;
   
    //弧度转角度
    //180/Pi = x/r
    NSLog(@"r is %.2f", 180 / M_PI * r);
}


//捏合
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
    *)longPress
{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"开始长按");
    }else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"结束长按");
    }
}


//旋转
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
    //获取旋转角度---弧度
    CGFloat r = rotation.rotation;
   
    //弧度转角度
    //180/Pi = x/r
    NSLog(@"r is %.2f", 180 / M_PI * r);
}


//捏合
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
0 0