28-手势 响应者链 手势识别器





----------------------------------------------------取得触摸点------------------------------------------------------

/**取得触摸手指*/
UITouch *touch = [touches anyObject]; 
/**手指触摸当前坐标*/
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
/**手指上一个坐标*/
CGPoint p1 =  [touch previousLocationInView:_view1];


//取得触摸所在的window
NSLog(@"%@",touch.window);

//取得触摸所在的view
NSLog(@"%@",touch.view);

/**取得点击的次数*/
NSInteger count = touch.tapCount;
   
/**开启多点触摸*/
self.view.multipleTouchEnabled = YES;
/**是否响应单个手指触摸*/
self.view.userInteractionEnabled = YES;


/**开始触摸时调用*/ 区分单击双击事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    /**取得点击的次数*/
    NSInteger count = touch.tapCount;
    if (count == 1) {
        [self performSelector:@selector(要执行的方法名) withObject:nil afterDelay:.2];
    }else if (count == 2) {       
       /**如果是双击,先取消单击点击事件再执行双击事件*/
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
       
        [self doubleTap];
    }
   
}


----------------------------------------------------单击双击手势-------------------------------------------------------

/**创建单击手势*/
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionOne)];
[self.view addGestureRecognizer:tap];
   
/**点击次数 默认为1*/
tap.numberOfTapsRequired = 1;

/**点击的手指个数 默认为1*/
tap.numberOfTouchesRequired = 1;

/**创建双击手势*/
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionTwo)];
[self.view addGestureRecognizer:tap2];

/**点击次数 默认为1*/
tap2.numberOfTouchesRequired = 1;

/**点击的手指个数 默认为1*/
tap2.numberOfTapsRequired = 2;


/**区别单击还是双击 原理:如果是双击,先取消单击点击事件再执行双击事件*/
[tap requireGestureRecognizerToFail:tap2];
    

- (void)tapActionOne
{
    NSLog(@"貌似是单击");
}

- (void)tapActionTwo
{
    NSLog(@"貌似是双击");
}



----------------------------------------------------- 清扫手势---- ----------------------------------------------------
/**创建清扫手势*/
UISwipeGestureRecognizer *sw = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swAction)];
[self.view addGestureRecognizer:sw];
  
/**手指个数 默认是1*/
sw.numberOfTouchesRequired = 1;
   
/**清扫方向 默认向右*/
sw.direction = UIDeviceOrientationLandscapeRight;

- (void)swAction
{
    NSLog(@"清扫了");
}



------------------------------------------------------ 平移手势---- ----------------------------------------------------
   /**平移手势*/
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [self.view addGestureRecognizer:pan];
   
    /**取得加手势的那个视图*/
    UIView *view = pan.view;
}

/**实时调用*/
- (void)panAction:(UIPanGestureRecognizer *)pan
{
    /**取得手指所在位置*/
    CGPoint point = [pan locationInView:self.view];
   
    /**让View跟随手指移动*/
    _view.center = point;

//    如果正在拖拽   实现放手势的view可以向右平移
    if (pan.state == UIGestureRecognizerStateChanged) {
       
//        在指定的view的坐标系中移动
        CGPoint p = [pan translationInView:pan.view];
        pan.view.transform = CGAffineTransformMakeTranslation(p.x, 0);
    }
//       取得偏移的坐标
     [pan setTranslation:CGPointZero inView:pan.view];
      - (CGPoint)velocityInView:(UIView *)view;//移动的速率
}



------------------------------------------------------ 长按手势---- ----------------------------------------------------
    /**创建长按手势*/
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
    [self.view addGestureRecognizer:longPress];
   
  
- (void)longAction:(UILongPressGestureRecognizer *)longPress
{
    /**判断状态 只执行一次长按操作*/
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按了");
    }
   
   
}



------------------------------------------------------ 旋转手势---- ----------------------------------------------------
   /**创建旋转手势*/
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:sw action:@selector(rotaAction:)];
    [self.view addGestureRecognizer:rotation];
   


- (void)rotaAction:(UIRotationGestureRecognizer *)rota
{
    /**取得图片*/
    UIImageView *imgView = (UIImageView *)rotationGesture.view;
   
    /**如果状态是在旋转*/
    if (rotationGesture.state == UIGestureRecognizerStateChanged) {
       
        imgView.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);
        /**如果停止手势*/
    }else if (rotationGesture.state == UIGestureRecognizerStateEnded) {
       
        [UIView animateWithDuration:.5 animations:^{
           
            /**图片恢复原位*/
            imgView.transform = CGAffineTransformIdentity;
        }];
    }
}




------------------------------------------------------ 捏合手势---- ----------------------------------------------------
    /**创建捏合手势*/
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [self.view addGestureRecognizer:pinchGesture];
   
}

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {

    //取得捏合的缩放倍数
    CGFloat scal = pinch.scale;
   
    //取得手势所在的视图
    UIImageView *imgView = (UIImageView *)pinch.view;
   
    if (pinch.state == UIGestureRecognizerStateChanged) {
       
        imgView.transform = CGAffineTransformMakeScale(scal, scal);
       
    }else if (pinch.state == UIGestureRecognizerStateEnded) {
   
        [UIView animateWithDuration:.5 animations:^{
           
            imgView.transform = CGAffineTransformIdentity;
        }];
    }
   
}




/**移动触摸时调用*/
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
        //实现scrollView的效果
       if (touches.count == 1) {
        UITouch *touch = [touches anyObject];
       
        //取得触摸的手指所在的视图位置(x,y)
        CGPoint point = [touch locationInView:self.view];
       
        //取得前一个坐标
        CGPoint lastPoint = [touch previousLocationInView:self.view];
       
        //取得X方向的偏移量
        CGFloat subX = point.x-lastPoint.x;
       
        _view1.frame = CGRectMake(CGRectGetMinX(_view1.frame)+subX, 90, 375, 200);
        _view2.frame = CGRectMake(CGRectGetMinX(_view2.frame)+subX, 90, 375, 200);
    }
   
   
    //捏合手势
    if(touches.count == 2) {
   
        //取得集合中的两个元素
        NSArray *array = [touches allObjects];
        UITouch *touch1 = array[0];
        UITouch *touch2 = array[1];
       
        //取得touch1所在的位置
        CGPoint point1 = [touch1 locationInView:self.view];
        //取得touch1前一个点所在的位置
        CGPoint lastPoint1 = [touch1 previousLocationInView:self.view];
       
        //取得touch2所在的位置
        CGPoint point2 = [touch2 locationInView:self.view];
        CGPoint lastPoint2 = [touch2 previousLocationInView:self.view];
       
        //  x1 y1    x2  y2  ((X1 - X2)平方 + (y1-y2)平方)开方
       
        CGFloat nowDistance = [self distancePoint:point1 withPoint:point2];
        CGFloat lastDistance = [self distancePoint:lastPoint1 withPoint:lastPoint2];
       
        if (nowDistance < lastDistance) {
            NSLog(@"缩小了");
        }else if(nowDistance > lastDistance) {
            NSLog(@"放大了");
        }
       
    }
}


//计算两个点得距离
- (CGFloat)distancePoint:(CGPoint)p1 withPoint:(CGPoint)p2 {

    CGFloat x1 = p1.x;
    CGFloat y1 = p1.y;
   
    CGFloat x2 = p2.x;
    CGFloat y2 = p2.y;
   
    CGFloat distance = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
   
    return distance;
   
}





/**结束触摸时调用*/
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值