gpt4 book ai didi

objective-c - Swift 和 Objective-C 之间的 UIGestureRecognizer

转载 作者:行者123 更新时间:2023-11-28 14:42:54 25 4
gpt4 key购买 nike

我有一个 Swift 应用程序 View ,添加了一些用 ObjC 编写的组件。

我正在以这种方式将 ObjC UIViewController 类添加到 Swift View ,无论如何它都可以工作(我不希望该 View 是弹出式 View ):

view.addSubview(myObjcViewController.view)

尽管 ObjC ViewController 的 View 已正确填充,没有任何问题,但在该 viewController 中编写的不同手势识别器不再工作:

UISwipeGestureRecognizer *upSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(upButtonTapped)];
upSwipe.numberOfTouchesRequired = 1;
upSwipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:upSwipe];

我尝试通过以下方式更改 gestureRecognizer 的添加,但均无效:

[self.parentViewController.view addGestureRecognizer:upSwipe];

// or

[[[[UIApplication sharedApplication] delegate] window] addGestureRecognizer:upSwipe];

请帮忙。

最佳答案

问题是您不能像这样简单地获取 View Controller 的 View 并将其填充到界面中:

view.addSubview(myObjcViewController.view)

相反,您必须精心制作舞蹈,以确保两个 View Controller (拥有 view 和拥有 myObjcViewController.view) 都保持一致的父子关系。

你不是在跳那支舞。因此,“子” View Controller 不复存在,并且没有包含任何代码供手势识别器与之对话。

关于objective-c - Swift 和 Objective-C 之间的 UIGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50436370/

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