iOS手势以及手势冲突问题

今天研究了一下iOS手势,手势包括点击,拖动,扫动,长按,捏合,旋转手势,下面上代码

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@property (nonatomic,strong)UIImageView * image;
@property (nonatomic,strong)UIAlertController * alertcontroller;
@property (nonatomic,strong)UIPinchGestureRecognizer * pinch;
@property (nonatomic,strong)UISwipeGestureRecognizer * swipright;
@property (nonatomic,strong)UISwipeGestureRecognizer * swipleft;
@property (nonatomic,strong)UILongPressGestureRecognizer * longpre;
@property (nonatomic,strong)UIRotationGestureRecognizer * rote;
@property (nonatomic,strong)UIPanGestureRecognizer * pan;
-(void)creatImage{

    _image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
    _image.frame = CGRectMake(0, 60, WIDTH, HEIGHT - 100);
    _image.userInteractionEnabled = YES;
    [self.view addSubview:_image];
    //解决在图片上滑动时拖动手势和轻扫手势的冲突
//    [_pan requireGestureRecognizerToFail:_swipright];
//    [_pan requireGestureRecognizerToFail:_swipleft];
    //解决拖动和长按手势之间的冲突
    [_longpre requireGestureRecognizerToFail:_pan];

}
/*
 1     UIGestureRecognizerStatePossible,   // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态
 3     UIGestureRecognizerStateBegan,      // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成
 4     UIGestureRecognizerStateChanged,    // 手势状态发生转变
 5     UIGestureRecognizerStateEnded,      // 手势识别操作完成(此时已经松开手指)
 6     UIGestureRecognizerStateCancelled,  // 手势被取消,恢复到默认状态
 7     UIGestureRecognizerStateFailed,     // 手势识别失败,恢复到默认状态
 8     UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded
 */
//创建点击
-(void)creatActionSheet{

    _alertcontroller = [UIAlertController alertControllerWithTitle:@"标题" message:@"啦啦" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        nil;

    }];
    [_alertcontroller addAction:action];

}

-(void)setTapGestureRecognizer{

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
    tap.numberOfTapsRequired = 1;//点击次数
    tap.cancelsTouchesInView = NO;
    tap.numberOfTouchesRequired = 1;//指头数  //3不行
    [self.view addGestureRecognizer:tap];

}

-(void)setPanGestureRecognizer{

    _pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panClick:)];
    _pan.minimumNumberOfTouches = 0.5;  //拖动的最短距离默认是0.5一般不要改
    _pan.maximumNumberOfTouches = 2;//拖动的最长距离
    _pan.delegate = self;
    [_image addGestureRecognizer:_pan];

}

-(void)setLongPressGestureRecognizer{

    _longpre = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longClick:)];
    _longpre.allowableMovement = 10; //在失败之前允许移动的
    _longpre.minimumPressDuration = 1;//长按的时间
    _longpre.delegate = self;
    [_image addGestureRecognizer:_longpre];

}

-(void)setPinchGestureRecognizer{

    _pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchClick:)];

    _pinch.delegate = self;
    [_image addGestureRecognizer:_pinch];
}

-(void)setRotationGestureRecognizer{

    _rote = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotClick:)];
    [_image addGestureRecognizer:_rote];
    _rote.delegate = self;
}

-(void)setSwipeGestureRecognizer{

    _swipright = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    _swipright.direction = UISwipeGestureRecognizerDirectionRight;
    [_image addGestureRecognizer:_swipright];
    _swipleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    _swipleft.direction = UISwipeGestureRecognizerDirectionLeft;
    [_image addGestureRecognizer:_swipleft];
}
#pragma mark Action
-(void)tapClick{

    NSLog(@"点击");
    [UIView animateWithDuration:0.2 animations:^{
        _image.frame = CGRectMake(0, 0, 300, 300);
    }];

}
-(void)panClick:(UIPanGestureRecognizer*)pan{
    //解决在图片上滑动时拖动手势和轻扫手势的冲突


    if (pan.state==UIGestureRecognizerStateChanged) {
        /*
         1.CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty)(平移:设置平移量)

         2.CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)(缩放:设置缩放比例)仅通过设置缩放比例就可实现视图扑面而来和缩进频幕的效果。

         3.CGAffineTransformMakeRotation(CGFloat angle)(旋转:设置旋转角度
         */
        CGPoint translation=[pan translationInView:self.view];//利用拖动手势的translationInView:方法取得在相对指定视图(这里是控制器根视图)的移动

        if (translation.x > 0) {
            [pan requireGestureRecognizerToFail:_swipright];
        }else{

            [pan requireGestureRecognizerToFail:_swipleft];
        }




        _image.transform = CGAffineTransformMakeTranslation(translation.x, translation.y);

        NSLog(@"%lf",[pan velocityInView:self.view].x);//移动的速率

    }else if(pan.state == UIGestureRecognizerStateEnded){

        [UIView animateWithDuration:0.5 animations:^{

            _image.transform = CGAffineTransformIdentity;

        }];

    }
}

-(void)longClick:(UILongPressGestureRecognizer*)longpress{

    //解决拖动和长按手势之间的冲突,这里必须是手势包含关系,比如拖动与骚动  长按与拖动
//    [_longpre requireGestureRecognizerToFail:_pan];

    if (longpress.state == UIGestureRecognizerStateBegan) {

        [self presentViewController:_alertcontroller animated:YES completion:^{


        }];
    }

}
-(void)pinchClick:(UIPinchGestureRecognizer*)pinch{

    CGFloat scale = pinch.scale;//获取当前的缩放比例
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, scale, scale); //在已缩放大小基础下进行累加变化;区别于:使用 CGAffineTransformMakeScale 方法就是在原大小基础下进行变化
    pinch.scale = 1.0;
}
-(void)rotClick:(UIRotationGestureRecognizer*)rot{

    if (rot.state == UIGestureRecognizerStateChanged) {

        _image.transform = CGAffineTransformMakeRotation(rot.rotation);

    }else if (rot.state == UIGestureRecognizerStateEnded){

        [UIView animateWithDuration:0.8 animations:^{
            _image.transform = CGAffineTransformIdentity;
        }];

    }
//    _image.transform = CGAffineTransformRotate(rot.view.transform, rot.rotation);
//    rot.rotation = 0.0;
}

-(void)swip:(UISwipeGestureRecognizer*)gesture{

    if (gesture == _swipright) {

        [self nextImage];
    }else if (gesture == _swipleft){

        [self lastImage];

    }

}
-(void)itemClick{

    SecondViewController * second = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:second animated:YES];
}
-(void)nextImage{

    _image.image = [UIImage imageNamed:@"2"];
}

-(void)lastImage{

    _image.image = [UIImage imageNamed:@"3"];
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值