UISB 手势

viewcontroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    // 定义试图对象
    UIImageView* _imageView;
    
}


@end

viewcontroller.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //加载图像
    UIImage* image = [UIImage imageNamed:@"a.jpg"];
    
    _imageView=[[UIImageView alloc]init];
    
    _imageView.image= image;
    
    _imageView.frame=CGRectMake(50, 80, 200, 300);
    
    [self.view addSubview:_imageView];
    //YES 开启交互事件开关
    //NO 不能接受响应事件 默认为NO
    
    _imageView.userInteractionEnabled=YES;
    //创建一个点击手势对象
    //UITapGestureRecognizer 点击手势类NO
    //功能 识别手势事件
    //P1 响应事件的拥有对象 self 表示当前视图控制器
    //P2 响应事件的函数
    
    
    UITapGestureRecognizer* tapOneGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOneAct:)];
    
    //表示手势识别事件的事件类型 几个手指点击时触发
    //默认 1
    tapOneGes.numberOfTapsRequired=1;
    
    //表示几个手指点击时触发事件函数
    //默认值为1
    tapOneGes.numberOfTouchesRequired=1;
    //将点击事件添加到视图中 ,视图即可响应事件
    [_imageView addGestureRecognizer:tapOneGes];
    
    UITapGestureRecognizer* tapTwo=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwo:) ];
    
    tapTwo.numberOfTapsRequired=2;
    
    tapTwo.numberOfTouchesRequired=1;
    [_imageView addGestureRecognizer:tapTwo];
    // 当单击操作遇到双击操作时 单机操作失效
    [tapOneGes requireGestureRecognizerToFail:tapTwo];
    
  
}
//事件响应函数
//参数 手势点击事件对象
-(void)tapOneAct:(UITapGestureRecognizer*)tap
{
    
    
    UIImageView* imageView = (UIImageView*) tap.view;
    
    imageView.frame = CGRectMake(0, 0, 320, 568);
    
}



-(void)tapTwo:(UITapGestureRecognizer*)tap
{
    NSLog(@"双次点击");
    _imageView.frame= CGRectMake(50, 80, 200, 300);
    
}

@end
原文地址:https://www.cnblogs.com/zhangqing979797/p/13697667.html