天天看点

UIGestureRecognizer,手势处理,点击,长按,拖动,左右划动

对于移动端,由于用户都是通过手指触摸来操作的,所以对于手势的处理是必不可少的。下面详细谈谈有哪几种手势操作。

1.点击,长按,拖动,左划,右划(上划下划)。

2.闲话不多说,直接看代码。代码Demo地址:https://github.com/gujinyue1010/iOS_UIGestureRecognizer

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.imageView1.userInteractionEnabled=YES;
    //1.点击手势
    //创建手势实例
    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture)];
    //设置手势点击数(不设置默认是一次)
    //tapGesture.numberOfTapsRequired=2;
    
    //为imageView1添加手势识别
    [self.imageView1 addGestureRecognizer:tapGesture];
    
    
    //2.长按手势
    UILongPressGestureRecognizer *longPressGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGesture)];
    longPressGesture.minimumPressDuration=1.0;
    [self.imageView1 addGestureRecognizer:longPressGesture];
    
    //3.拖手势
    UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
    [self.imageView1 addGestureRecognizer:panGesture];
    
    //4.左划手势(整个屏幕给个划动手势)
    UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
    swipeLeftGesture.direction=UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeLeftGesture];
    
    //5.右划手势
    UISwipeGestureRecognizer *swipeRightGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
    swipeRightGesture.direction=UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeRightGesture];
    
}

-(void)handleTapGesture
{
    NSLog(@"双击了图片");
}

-(void)handleLongPressGesture
{
    NSLog(@"长按了图片");
}

-(void)handlePanGesture:(UIPanGestureRecognizer *)sender
{
    //平衡
    CGPoint netTranslation;
    
   //得到拖过程中的xy坐标
    CGPoint translation=[(UIPanGestureRecognizer *)sender translationInView:self.imageView1];
    
    //平移图片
    sender.view.transform=CGAffineTransformMakeTranslation(netTranslation.x+translation.x, netTranslation.y
                                                           +translation.y);
    
    if(sender.state==UIGestureRecognizerStateEnded)
    {
        netTranslation.x+=translation.x;
        netTranslation.y+=translation.y;
    }
    
    NSLog(@"在拖动");
}
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *)sender
{
    NSLog(@"%lu",(unsigned long)sender.direction);
    if(sender.direction==UISwipeGestureRecognizerDirectionUp)
    {
        NSLog(@"向上");
    }
    else if(sender.direction==UISwipeGestureRecognizerDirectionLeft)
    {
        NSLog(@"向左");
    }
    else if(sender.direction==UISwipeGestureRecognizerDirectionDown)
    {
        NSLog(@"向下");
    }
    else if(sender.direction==UISwipeGestureRecognizerDirectionRight)
    {
        NSLog(@"向右");
    }
}