美文网首页
iOS-UIGestureRecognizer(手势)

iOS-UIGestureRecognizer(手势)

作者: 学长的日常 | 来源:发表于2016-07-28 12:48 被阅读75次
  • 手势的创建及方法的实现

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;
   }
}

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

相关文章

  • iOS-UIGestureRecognizer(手势)

    手势的创建及方法的实现 1、点击手势 3、长按手势 5、旋转手势 6、拖拽手势 常用代理方法 代理方法都是可选的,...

  • 手势

    点击手势 捏合手势 旋转手势 轻扫手势 拖拽手势 边缘平移手势 长按手势

  • iOS-手势详细参数说明

    敲击手势 长按手势 滑动手势 拖动手势 旋转手势 捏合手势 两种手势作用在同一个视图

  • 【iOS学习】——手势识别

    iOS 手势 手势需要开启用户交互 点击手势 单击手势 双击手势 添加 numberOfTapsRequired...

  • Swift - UIGestureRecognizer 各种手势

    1、点击手势2、拖动手势3、长按手势4、滑动手势5、捏合手势6、旋转手势 完整代码

  • iOS七种手势详解

    1、轻拍手势 2、捏合手势 3、旋转手势 4、平移手势 5、边缘轻扫手势 6、长按手势 7、轻扫手势 给image...

  • iOS手势总结

    1.轻拍手势 2.长按手势 3.轻扫手势 4.平移手势 5.捏合手势 6.旋转手势 7.边缘手势

  • iOS 手势

    修改时间: 2016-12-19修改次数: 0 手势传递 点击手势 捏合手势 轻扫手势 拖动手势 长按手势

  • iOS手势操作

    iOS手势有六种 手势类型: 手势状态: 创建View添加手势 1.轻点手势( UITapGestureRecog...

  • Vue手势

    点击手势 滑动手势 手势的方法

网友评论

      本文标题:iOS-UIGestureRecognizer(手势)

      本文链接:https://www.haomeiwen.com/subject/koonjttx.html