美文网首页
UIPanGestureRecognizer 滑动手势,侧边vi

UIPanGestureRecognizer 滑动手势,侧边vi

作者: 阳光下的叶子呵 | 来源:发表于2021-01-26 15:00 被阅读0次
    #pragma mark -- 筛选 分类
    - (void)clickFilterType {
        
        if (!_backView) {
            // 背景view
            _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
            _backView.backgroundColor = RGBA(0, 0, 0, 0.3);
            [self.view addSubview:self.backView];
        }
        
        if (!_filterBackView) {
            _filterBackView = [[VideoFilterBackView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
            _filterBackView.backgroundColor = [UIColor clearColor];
            [self.backView addSubview:self.filterBackView];
            
            // 平移手势
            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
            [self.filterBackView addGestureRecognizer:pan];
            
        }
    }
    
    - (void)panAction:(UIPanGestureRecognizer *)panGesture {
        
        _canLeft = YES;
        _canRight = YES;
        
    //    panGesture = (UIPanGestureRecognizer *)self.filterBackView;
        
        // 获取平移手势移动后, 在相对坐标中的偏移量
        CGPoint transP = [panGesture translationInView:panGesture.view];
        // 偏移的速度
        CGPoint velocity = [panGesture velocityInView:panGesture.view];
        
        // 禁止左滑的情况(在最左边时)
        if (panGesture.view.frame.origin.x <= 0 && transP.x <= 0) {
            _canLeft = NO;
        }
        
        // 禁止右滑的情况(在最右边时)
        if (panGesture.view.frame.origin.x >= ScreenWidth && transP.x >= 0) {
            _canRight = NO;
        }
        
        // view滑动的条件
        if (_canLeft && _canRight && panGesture.view.frame.origin.x >= 0 && panGesture.view.frame.origin.x <= ScreenWidth) {
            
            // 移动
            panGesture.view.frame = CGRectMake( transP.x + panGesture.view.frame.origin.x, 0, ScreenWidth, ScreenHeight);
            
        }
        
        
        
    //    self.filterBackView.frame = CGRectMake( transP.x + self.filterBackView.frame.origin.x, 0, ScreenWidth, ScreenHeight);
        // 每次都需要复位
        [panGesture setTranslation:CGPointZero inView:panGesture.view];
        
        // 松开手指时判断滑动趋势让其归位
        if (panGesture.state == UIGestureRecognizerStateEnded) {
            if (panGesture.view.frame.origin.x < ScreenWidth/2) { // 滑到一半位置
    //            panGesture.view.frame.origin.x = 0;
                panGesture.view.frame = CGRectMake( 0, 0, ScreenWidth, ScreenHeight);
            } else if (panGesture.view.frame.origin.x > ScreenWidth/2) {
    //            panGesture.view.frame.origin.x == ScreenWidth;
                panGesture.view.frame = CGRectMake(ScreenWidth, 0, ScreenWidth, ScreenHeight);
                
                // 右滑,隐藏
                [self.filterBackView removeFromSuperview];
                self.filterBackView = nil;
                
                [self.backView removeFromSuperview];
                self.backView = nil;
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:UIPanGestureRecognizer 滑动手势,侧边vi

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