gpt4 book ai didi

ios - UISwipeGestureRecognizer 只有一个方向工作

转载 作者:可可西里 更新时间:2023-11-01 05:02:45 25 4
gpt4 key购买 nike

所以我用 pageControl 创建一个页面(这是一个有多个 View 的页面,用点表示你在哪个页面),我的代码在 viewDidLoad 中看起来像下面这样:

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
UIView *temp = [[UIView alloc]initWithFrame:self.view.frame];
temp.backgroundColor = [UIColor clearColor];
[temp addGestureRecognizer:swipe];
[self.view addSubview:temp];

在 swipeAction 选择器中我有:

- (void)swipeAction: (UISwipeGestureRecognizer *)sender{
NSLog(@"Swipe action called");
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
//Do Something
}
else if (sender.direction == UISwipeGestureRecognizerDirectionRight){
//Do Something Else
}
}

令我惊讶的是,此方法仅在您向右滑动时有效(即调用 else if block )。当您向左滑动时,swipeAction 甚至都不会被调用!这很奇怪,为什么会发生这种情况,我应该如何更改我的代码?任何答复表示赞赏。非常感谢!

最佳答案

这里有几件事您应该注意。首先,您必须为要观察的每个方向创建一个手势。不过这没什么大不了的,因为您可以简单地为它们提供相同的选择器,这就像一个手势用于两个方向。

UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;

UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;


UIView *temp = [[UIView alloc]initWithFrame:self.view.frame];

temp.backgroundColor = [UIColor clearColor];
[temp addGestureRecognizer:leftSwipe];
[temp addGestureRecognizer:rightSwipe];

[self.view addSubview:temp];

其次,您从未指定手势的方向,使其默认为右(或方向枚举中的 1)

来自documentation :

The default direction is UISwipeGestureRecognizerDirectionRight. See descriptions of UISwipeGestureRecognizerDirection constants for more information.

typedef enum {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
} UISwipeGestureRecognizerDirection;

关于ios - UISwipeGestureRecognizer 只有一个方向工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24249171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com