草庐IT

ios - UIScreenEdgePanGestureRecognizer 无法识别右边缘的手势

coder 2023-09-27 原文

我遇到了一个问题,我定义了一个 UIScreenEdgePanGestureRecognizer 来检测出现在我设备右边缘的平移手势,但该手势偶尔会被识别:

我有以下代码:

 _swipeInLeftGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeInFromRightEdge:)];
 _swipeInLeftGestureRecognizer.minimumNumberOfTouches = 1;
 _swipeInLeftGestureRecognizer.maximumNumberOfTouches = 1;
 [_swipeInLeftGestureRecognizer setEdges:UIRectEdgeRight];
 [self.view addGestureRecognizer:_swipeInLeftGestureRecognizer];

- (void)handleSwipeInFromRightEdge:(UIGestureRecognizer*)sender
{
    NSLog(@"swipe from right edge!!!!");
}

手势附加到一个没有任何内容的 View 上。

我错过了什么吗?

最佳答案

我已经设法创建了一个解决方法。这非常简单。我已将 UIWindow 子类化并使用 touchesBegan/touchesMoved/等。模拟手势识别的方法。

它有效。 UIWindow 不会自动旋转,因此我必须相应地变换触摸坐标。

这是我的转换版本:

- (CGPoint)transformPoint:(CGPoint)point {
    CGPoint pointInView = point;

    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        pointInView.x = self.bounds.size.width - pointInView.x;
        pointInView.y = self.bounds.size.height - pointInView.y;
    } else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft) {
        CGFloat x = pointInView.x;
        CGFloat y = pointInView.y;
        pointInView = CGPointMake(self.bounds.size.height - y, x);
    } else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) {
        CGFloat x = pointInView.x;
        CGFloat y = pointInView.y;
        pointInView = CGPointMake(y, self.bounds.size.width - x);
    }
    return pointInView;
}

关于ios - UIScreenEdgePanGestureRecognizer 无法识别右边缘的手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20426319/

有关ios - UIScreenEdgePanGestureRecognizer 无法识别右边缘的手势的更多相关文章

随机推荐