(iOS)手势识别的简单使用

作者: 三生石畔 | 来源:发表于2016-07-10 12:06 被阅读274次
  • (ios)手势包括以下6种
UITapGestureRecognizer //点击手势
UILongPressGestureRecognizer // 长按手势
UISwipeGestureRecognizer    // 轻扫手势
UIRotationGestureRecognizer // 旋转手势
UIPinchGestureRecognizer    //捏合手势
UIPanGestureRecognizer      //拖拽手势
  • UIGestureRecognizerDelegate
// 是否允许开始触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return NO;
}

// 是否允许同时支持多个手势,默认是不支持多个手势
// 返回yes表示支持多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

// 是否允许接收手指的触摸点
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    // 获取当前的触摸点
    CGPoint curP = [touch locationInView:self.imageView];
    //下面表示当触摸点在self.imageView 在中心点左面点击不触发事件,在右面则触发点击事件
    if (curP.x < self.imageView.bounds.size.width * 0.5) {
        return NO;
    }else{
        return YES;
    }
}
  • 手势触发事件需要注意的地方

// 创建点按手势
- (void)setUpTap
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    
    tap.delegate = self;
  //连续点击两次
    tap.numberOfTapsRequired = 2;
    //需要两跟手指一起敲击
    tap.numberOfTouchesRequired =2;
    
    [_imageView addGestureRecognizer:tap];
}

- (void)tap:(UITapGestureRecognizer *)tap
{
    NSLog(@"%s",__func__);
}
//创建长按手势
- (void)setUpLongPress
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    
    [self.imageView addGestureRecognizer:longPress];
}


- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
    // 默认会触发两次,所以需要加上状态去限制
    if (longPress.state == UIGestureRecognizerStateBegan) {
        
        NSLog(@"%s",__func__);
    }
}
//创建轻扫手势
- (void)setUpSwipe
{
    // 默认轻扫的方向是往右
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
    //设置轻扫手势的方向
    swipe.direction = UISwipeGestureRecognizerDirectionUp;
    
    [self.imageView addGestureRecognizer:swipe];
    
    // 如果以后想要一个控件支持多个方向的轻扫,必须创建多个轻扫手势,一个轻扫手势只支持一个方向
    // 默认轻扫的方向是往右
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
    
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    
    [self.imageView addGestureRecognizer:swipeDown];

    
}

- (void)swipe
{
    NSLog(@"%s",__func__);
}
//创建旋转手势
- (void)setUpRotation
{
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    rotation.delegate = self;
    [self.imageView addGestureRecognizer:rotation];
}

// 默认传递的旋转的角度都是相对于最开始的位置
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
    
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
    // 可以不加下面这句复位的代码试一下效果
    // 复位
    rotation.rotation = 0;
    
    // 获取手势旋转的角度
    NSLog(@"%f",rotation.rotation);
}
//创建捏合手势
- (void)setUpPinch
{
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    pinch.delegate = self;
    [self.imageView addGestureRecognizer:pinch];
}
//默认传递的的缩放比例都是相对于最开始的大小
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
    
    // 复位
    pinch.scale = 1;
}
//创建拖拽手势
- (void)setUpPan
{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    
    
    [self.imageView addGestureRecognizer:pan];
}
//一样是默认是相对于最初位置
- (void)pan:(UIPanGestureRecognizer *)pan
{
    // 获取手势的触摸点
   // CGPoint curP = [pan locationInView:self.imageView];
    
    // 移动视图
    // 获取手势的移动,也是相对于最开始的位置
    CGPoint transP = [pan translationInView:self.imageView];
    
    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
    
    // 复位
    [pan setTranslation:CGPointZero inView:self.imageView];
    
  //  NSLog(@"%@",NSStringFromCGPoint(curP));
}

相关文章

网友评论

本文标题:(iOS)手势识别的简单使用

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