轻扫手势/平移长按手势

Posted pengyuan_D

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了轻扫手势/平移长按手势相关的知识,希望对你有一定的参考价值。

(1)创建故事板

(2)在ViewController.h中设置全局_myView

(3)ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad

    [super viewDidLoad];

    _myView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 320, 400)];
    _myView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_myView];
    
    /*__________________________________手势的使用_____________________________________*/
    //_________________________点击手势__________________________
    //单击
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1Action:)];
    //设置点击的次数,默认是1
    tap1.numberOfTapsRequired = 1;
    //设置点击的手指个数
    tap1.numberOfTouchesRequired = 1;
    //将点击的手势添加到视图上
    [_myView addGestureRecognizer:tap1];
    
    //双击
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap2Action:)];
    //设置点击的次数
    tap2.numberOfTapsRequired = 2;
    //设置手指个数
    tap2.numberOfTouchesRequired = 1;
    //添加到视图上
    [_myView addGestureRecognizer:tap2];
    
    //如果我们双击则取消单击相应事件(注意:需要取消的手势放在前面)
    [tap1 requireGestureRecognizerToFail:tap2];
    
    //_________________________轻扫手势__________________________
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    //设置轻扫的方向
    /*
     UISwipeGestureRecognizerDirectionRight  右(默认)
     UISwipeGestureRecognizerDirectionLeft      左
     UISwipeGestureRecognizerDirectionUp    上
     UISwipeGestureRecognizerDirectionDown  下
     */
    swipe.direction = UISwipeGestureRecognizerDirectionUp;
    [_myView addGestureRecognizer:swipe];
    
    //_________________________平移手势__________________________
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [_myView addGestureRecognizer:pan];

    //_________________________长按手势__________________________
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    //设置长按的最短时间
    longPress.minimumPressDuration = 2;
    [_myView addGestureRecognizer:longPress];
    
    //_________________________旋转手势__________________________
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [_myView addGestureRecognizer:rotation];
    
    //_________________________捏合手势__________________________
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [_myView addGestureRecognizer:pinch];
    


#pragma mark - 手势响应事件

//单击相应的事件
- (void)tap1Action:(UITapGestureRecognizer *)tap 

    //取得点击手指的个数
//    NSInteger count = tap.numberOfTouches;
    
    CGPoint point = [tap locationInView:_myView];
    NSString *str = NSStringFromCGPoint(point);
    
    NSLog(@"单击了,坐标是:%@",str);
    


- (void)tap2Action:(UITapGestureRecognizer *)tap 
    NSLog(@"双击了");


- (void)swipeAction:(UISwipeGestureRecognizer *)swipe 

    if (swipe.direction == UISwipeGestureRecognizerDirectionUp) 
        NSLog(@"轻扫了");
    
    


//手指移动的时候会不停的调用这个方法:注意此时轻扫不能使用了
- (void)panAction:(UIPanGestureRecognizer *)pan 

    
    
    //可以通过手势取得手势所在的视图
    CGPoint point = [pan locationInView:pan.view];
//    NSString *str = NSStringFromCGPoint(point);
    
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(point.x, point.y, 5, 5)];
//    view.backgroundColor = [UIColor redColor];
//    [pan.view addSubview:view];
    
//    NSLog(@"平移坐标:%@",str);
    


- (void)longPressAction:(UILongPressGestureRecognizer *)longPress 

    if (longPress.state == UIGestureRecognizerStateBegan) 
        NSLog(@"长按开始了");
    else if (longPress.state == UIGestureRecognizerStateEnded) 
        NSLog(@"长按结束了");
    


//旋转的时候一直调用
- (void)rotationAction:(UIRotationGestureRecognizer *)rt 

//    NSLog(@"旋转了");
    
    /*
     180/M_PI = 角度/r;
     
     角度 = 180*r/M_PI;
     */
    
    CGFloat r = rt.rotation;
    
//    角度
    float jd = 180/M_PI*r;
    
    NSLog(@"jd:%.2f",jd);
    


- (void)pinchAction:(UIPinchGestureRecognizer *)pinch 

//    NSLog(@"捏合");
    //取得缩放的倍数
    CGFloat scale = pinch.scale;
    
//    NSLog(@"scale:%.2f",scale);
    
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, scale, scale);
    


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


@end



以上是关于轻扫手势/平移长按手势的主要内容,如果未能解决你的问题,请参考以下文章

iOS 手势操作:拖动捏合旋转点按长按轻扫自定义

flutter web中如何区分手指触摸手势和鼠标指针手势?

平移手势使视图在拖动时从手指跳开

一起识别长按和平移手势识别器

使用平移手势,单次触摸后忽略触摸?

UICollectionView 中的长按和平移手势