iOS-UIGestureRecognizer(手势)

  • 手势的创建及方法的实现

1、点击手势

//创建点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//添加代理
    tap.delegate = self;
//添加手势
    [self.imageView addGestureRecognizer:tap];
#pragma mark -
#pragma mark - 点击手势实现的方法
-(void)tapAction:(UITapGestureRecognizer *)tap{
   
   NSLog(@"点击了");
}

2、轻扫手势
  UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
  swip.direction = UISwipeGestureRecognizerDirectionUp;
  
  [self.imageView addGestureRecognizer:swip];

#pragma mark -
#pragma mark - 轻扫手势实现方法
-(void)swipAction:(UISwipeGestureRecognizer    *)swip{
   
    NSLog(@"清扫了");
 
}

3、长按手势

    UILongPressGestureRecognizer *longPres =   [[UILongPressGestureRecognizer   alloc]initWithTarget:self  action:@selector(longAction:)];
   [self.imageView  addGestureRecognizer:longPres];
#pragma mark -
#pragma mark - 长按手势实现方法
//默认长按会有两次触发效果,即点击时和取消点击时都会调用实现的方法
-(void)longAction:(UILongPressGestureRecognizer *)longPres{
 //设置点击时处理
    if (longPres.state == UIGestureRecognizerStateBegan) {
          NSLog(@"长按了");
    }
}
4、捏合手势
 UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinAction:)];
    [self.imageView addGestureRecognizer:pin];
#pragma mark -
#pragma mark - 捏合手势
- (void)pinAction:(UIPinchGestureRecognizer *)pin{
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pin.scale, pin.scale);
//复位
   pin.scale = 1;
}

5、旋转手势

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
    rotation.delegate = self;
    [self.imageView addGestureRecognizer:rotation];
#pragma mark -
#pragma mark - 旋转手势
-(void)rotationAction:(UIRotationGestureRecognizer *)rotation{
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotaion.rotation);
   //复位
   rotaion.rotation = 0;
}

6、拖拽手势

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
   [self.imageView addGestureRecognizer:pan];
#pragma mark -
#pragma mark - 拖拽手势
-(void)panAction:(UIPanGestureRecognizer *)pan{
//获取手势的移动,也是相对于最开始的位置
  CGPoint transP = [pan translationInView:self.imageView];
  self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
//复位
  [pan setTranslation:CGPointZero inView:self.imageView];
}
  • 常用代理方法
//是否同时支持多种手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
   return YES;
}
//是否允许开始点击
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
}
//设置点击的范围
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//获取当前的触摸点
   CGPoint curp = [touch locationInView:self.imageView];
   if (curp.x <= self.imageView.bounds.size.width*0.5) {
       return NO;
   }else{
       
       return YES;
   }
}

代理方法都是可选的,想通过代理方法实现手势的某个效果 ,就把该手势设置代理。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容