美文网首页手势
iOS-滑动手势UIPanGestureRecognizer左右

iOS-滑动手势UIPanGestureRecognizer左右

作者: Simple_Code | 来源:发表于2020-01-21 11:51 被阅读0次
    - (void)panAction:(UIPanGestureRecognizer *)pan{
    
        _canLeft = YES;
        _canRight = YES;
    
        //点相对于上一个点的位置
        CGPoint moviePoint = [pan translationInView:pan.view];
    
        //点的速度(正负可判断滑动趋势)
        CGPoint velocity = [pan velocityInView:pan.view];
    
        //侧滑的范围
        CGFloat instance = kScreenWidth - 100;
    
        //禁止左划的情况(在最左边时)
        if (pan.view.left <= 0 && moviePoint.x <= 0) {
            _canLeft = NO;
        }
    
        //禁止右划得情况(在最右边时)
        if (pan.view.left >= instance && moviePoint.x >= 0) {
            _canRight = NO;
        }
    
        //页面可以滑动的条件
        if (_canRight && _canLeft && pan.view.left >= 0 && pan.view.left <= instance){
    
            //移动
            pan.view.center =
    
            CGPointMake(pan.view.center.x + moviePoint.x, pan.view.center.y);
        }
    
        //每次都需要复位
        [pan setTranslation:CGPointZero inView:pan.view];
    
        //松开手指时判断滑动趋势让其归位
    
        if (pan.state == UIGestureRecognizerStateEnded) {
            if (pan.view.left < 10) {
                pan.view.left = 0;
            } else if(pan.view.left > instance - 10){
                pan.view.left = instance;
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:iOS-滑动手势UIPanGestureRecognizer左右

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