UIGesture手势基础:UITapGestureRecognizer

UITapGestureRecognizer

  • alloc 创建对象分配内存
  • initWithTarget 初始化响应事件,可传参
  • .numberOfTapsRequired 手指触屏次数 单击/双击/三击/...
  • .numberOfTouchedRequired 触屏的手指数 单指/双指/....
  • requireGestureRecognizerToFail 设置手势冲突时失效

UIImageView *iView

  • .userInteractionEnabled=YES 开启交互事件响应开关!!!UIImageView默认是没有开启的
  • addGestureRecognizer 为图像视图添加手势响应

记录一个例子:图像视图单指单击放大,双击缩小

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed:@"icon1"];
    UIImageView *iView = [[UIImageView alloc]initWithImage:image];
    iView.frame = CGRectMake(100, 200, 80, 80);
    
    [self.view addSubview:iView];
    //开启交互事件响应开关!!!!
    iView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tapOne = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapOneAction:)];
    //单击
    tapOne.numberOfTapsRequired = 1;
    //单指
    tapOne.numberOfTouchesRequired = 1;
    [iView addGestureRecognizer:tapOne];
    
    UITapGestureRecognizer *tapTwo = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTwoAction:)];
    //单指双击
    tapTwo.numberOfTapsRequired = 2;
    tapTwo.numberOfTouchesRequired = 1;
    [iView addGestureRecognizer:tapTwo];
    //当单击操作遇到双击操作时失效
    [tapOne requireGestureRecognizerToFail:tapTwo];
}
-(void)tapTwoAction:(UITapGestureRecognizer *)tapTwo{
   UIImageView *iView = (UIImageView *)tapTwo.view;
   //设置动画过渡效果
   [UIView beginAnimations:nil context:nil];
   //设置动画过渡时间
   [UIView setAnimationDuration:2];
   iView.frame = CGRectMake(100, 200, 80, 80);
   [UIView commitAnimations];
}

-(void)tapOneAction:(UITapGestureRecognizer *)tap{
   UIImageView *iView =(UIImageView *) tap.view;
   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationDuration:2];
   iView.frame = CGRectMake(0, 0, 320, 560);
   [UIView commitAnimations];
   NSLog(@"单击事件!");
}

你可能感兴趣的:(UIGesture手势基础:UITapGestureRecognizer)