gpt4 book ai didi

ios - 将手势识别器限制为仅一个特定的 UIView?

转载 作者:行者123 更新时间:2023-11-28 22:07:01 26 4
gpt4 key购买 nike

我在 myViewController 上有一个名为 myView 的 UIView。我有一个名为 swipeLeft(下面的代码)的 UIGestureRecognizer,它可以检测用户何时向左滑动。

问题是:myViewController 在整个屏幕上识别相同的手势并执行另一个 Action 。因此,当您在 myViewController 的这个特定区域中 swipeLeft 时,我希望我的应用程序执行 myMethod,该区域是 myView.

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(myMethod:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delaysTouchesBegan = YES;
[self.myView addGestureRecognizer:swipeLeft];

更多细节:我正在使用 RESideMenu myViewController 是右侧菜单,因此当它可见时,myViewController 的整个 View 可以识别各个方向的滑动。我想更改此特定 UIView myView 中的识别器。

谢谢!

最佳答案

首先,您需要将滑动手势添加到您的 View Controller 头文件中。

@property (strong, nonatomic) UISwipeGestureRecognizer *swipeLeft;

如果你查看 DEMORootViewController.m,你会看到这个调用:

self.rightMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"rightMenuViewController"];

这将调用您的 awakeFromNib,这是您做某事的第一次机会。在这里您将创建滑动手势。你还不能将它添加到你的 View 中,因为此时你的 socket 还没有设置。第一次设置它们是在 viewDidLoad 中,因此您可以在此处将手势添加到 View 中。所以将其添加到您的 View Controller 实现文件中

- (void)awakeFromNib
{
self.swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(myMethod:)];
}

- (void)viewDidLoad
{
[super viewDidLoad];

[self.swipeView addGestureRecognizer:self.swipeLeft];
}

- (void)myMethod:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"Did swipe") ;
}

最后,您需要在 RESideMenu.m 中告诉平移手势在我们的 swipeLeft 手势出现时失败。这样做的方法是将以下代码添加到 RESideMenu.m 的第 220 行。那是在 viewDidLoad 方法的末尾。

if ([self.rightMenuViewController isKindOfClass:[DEMOVC class]]) {
DEMOVC *rightVC = (DEMOVC *)self.rightMenuViewController;
if (rightVC.swipeLeft) {
[panGestureRecognizer requireGestureRecognizerToFail:rightVC.swipeGesture];
} } }

这是假设您的自定义 VC 称为 DEMOVC。您还需要像这样将自定义 VC 导入 RESideMenu.m:

#import "DEMOVC.h"

请告诉我这是否适合您,如果还有其他事情我可以帮助您。

关于ios - 将手势识别器限制为仅一个特定的 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23722110/

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