gpt4 book ai didi

iOS:如何从 Swift 类触发 Obj-C 中的 UITapGestureRecognizer 和 UIPanGestureRecognizer?

转载 作者:行者123 更新时间:2023-11-30 11:45:23 25 4
gpt4 key购买 nike

我使用 SWRevealViewController,并且我想在 Obj-C 类和 Swift 类的混合中使用它。它在 Swift 中的 Obj-C 类中运行良好,但我的问题是获取左侧菜单的滑动方法不起作用(按钮可以工作但不能滑动):

Obj-C 代码添加到 viewController 的开头以使其工作:

   //Side MEnu
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
SWRevealViewController *revealVC = self.revealViewController;
if (![revealVC uniqueNavController])
{
[revealVC setUniqueNavController:self.navigationController];
}
[_menuAction addTarget:self.revealViewController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
[_menuAction setImage:[UIImage imageNamed:@"Menu_burger_Clicked.png"] forState:UIControlStateHighlighted];

UITapGestureRecognizer *tap = [revealViewController tapGestureRecognizer];
tap.delegate = self;
[self.view addGestureRecognizer:tap];
}

Swift 代码添加到 viewController 的开头以使其工作:

 //Side MEnu
let revealViewController: SWRevealViewController? = SWRevealViewController()
if revealViewController != nil {
let revealVC: SWRevealViewController? = revealViewController
if revealVC?.uniqueNavController == nil {
revealVC?.uniqueNavController = navigationController
}
menuAction.addTarget(revealViewController, action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)
view.addGestureRecognizer(revealViewController!.panGestureRecognizer())
menuAction.setImage(UIImage(named: "Menu_burger_Clicked.png"), for: .highlighted)

let tap: UITapGestureRecognizer? = revealViewController?.tapGestureRecognizer
tap?.delegate = self as? UIGestureRecognizerDelegate
if let aTap = tap {
self.view.addGestureRecognizer(aTap)
}
}

这里是SWRevealViewController.m中要触发的函数

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)recognizer
{
// only allow gesture if no previous request is in process
if ( _animationQueue.count == 0 )
{
if ( recognizer == _panGestureRecognizer )
return [self _panGestureShouldBegin];

if ( recognizer == _tapGestureRecognizer )
return [self _tapGestureShouldBegin];
}

return NO;
}

enter image description here

这在快速 View 中有效,但这不是我想要的。我希望从该快速 View 中触发gestureRecognizer Obj-c SWRevealViewController:

        //PanGesture
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(recognizer:)))
self.view.addGestureRecognizer(panGestureRecognizer)

//TapGesture
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(recognizer:)))
self.view.addGestureRecognizer(tapGestureRecognizer)

func handlePan(recognizer: UIPanGestureRecognizer) {
print("PAN WORKING!!!!")
}
func handleTap(recognizer: UIPanGestureRecognizer) {
print("TAP WORKING!!!!")
}

提前致谢...

最佳答案

解决办法如下:

@objc class PlanningViewController: UIViewController, UIGestureRecognizerDelegate, UINavigationControllerDelegate {


#if SIDE_MENU
//Side MEnu
let revealViewController: SWRevealViewController = SWRevealViewController()
let revealVC: SWRevealViewController = revealViewController
if revealVC.uniqueNavController == nil
{
revealVC.uniqueNavController = navigationController
}
menuAction.addTarget(revealViewController, action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)
menuAction.setImage(UIImage(named: "Menu_burger_Clicked.png"), for: .highlighted)

if self.revealViewController() != nil
{
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer)
}
#endif

在这里找到:Tutorial

关于iOS:如何从 Swift 类触发 Obj-C 中的 UITapGestureRecognizer 和 UIPanGestureRecognizer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48890743/

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