ios - UIScreenEdgePanGestureRecognizer 不能检测到状态?
迷茫
迷茫 2017-04-17 17:56:26
[iOS讨论组]

我有一个侧滑菜单,使用UIScreenEdgePanGestureRecognizer来从屏幕边缘,由左至右滑动打开,由右至左滑动关闭。

现在的问题是,我从右至左快速滑动时(或者使用真机调试,手指从右向左,手机重新触碰屏幕边缘时),侧滑菜单就会停留在半打开的状态,这个时候检测不到if(sender.state == UIGestureRecognizerStateEnded).

我的代码如下:

self.panGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDo:)];

-(void)panGestureDo:(UIScreenEdgePanGestureRecognizer *)sender{
// NSLog(@"%ld",(long)sender.state);

if(sender.state == UIGestureRecognizerStateBegan){
    self.startLocation = [sender locationInView:self.view];
}

self.currentLocation = [sender locationInView:self.view];
CGFloat distance = self.startLocation.x - self.currentLocation.x;

if(distance < 0){
    [self.mainView addSubview:self.blurEffectView];
    [self.blurEffectView addGestureRecognizer:self.tapGesture];
    self.blurEffectView.alpha = (-distance)/100;
    self.leftMenu.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.leftMenuWidth, self.view.frame.size.height);
    self.mainView.frame =CGRectMake(self.view.frame.origin.x - distance, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);

    if (self.mainView.frame.origin.x > 97) {
        self.mainView.frame =CGRectMake(self.view.frame.origin.x + self.leftMenuWidth, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
        [self openMenuHalfAnimation];
    }

    if (self.mainView.frame.origin.x < 3) {
        self.mainView.frame =CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
        [self closeMenuHalfAnimation];
    }
        if(sender.state == UIGestureRecognizerStateEnded){

        //当手势结束时,判断侧滑菜单的位置,自动打开或者关闭。
        //但是当从右向左滑动速度过快,就不会进入这个if条件里面。

            NSLog(@"UIGestureRecognizerStateEnded");
            if (self.mainView.frame.origin.x > 50) {
                [self openMenuHalfAnimation];
            }else{
                [self closeMenuHalfAnimation];
            }

        }
        
        //下面的我都试过,检测不到滑动过快的情况。
        if(sender.state == UIGestureRecognizerStateCancelled){
            NSLog(@"UIGestureRecognizerStateCancelled");
        }
        if(sender.state == UIGestureRecognizerStateFailed){
            NSLog(@"UIGestureRecognizerStateFailed");
        }
        if(sender.state == UIGestureRecognizerStatePossible){
            NSLog(@"UIGestureRecognizerStatePossible");
        }
//        if(sender.state == UIGestureRecognizerStateChanged){
//            NSLog(@"UIGestureRecognizerStateChanged");
//        }
        if(sender.state == UIGestureRecognizerStateBegan){
            NSLog(@"UIGestureRecognizerStateBegan");
        }
        if(sender.state == UIGestureRecognizerStateRecognized){
            NSLog(@"UIGestureRecognizerStateRecognized");
        }
    }
}

我还尝试过这些,但是都没有被调用。

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"touches end");
}

-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"touches canceled");
}
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(1)
巴扎黑

楼主 解决了吗

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号