6

我在标签栏上使用长按手势。但我只需要一个特定标签栏项目的长按手势。

我怎么解决这个问题?我可以自定义标签栏中的长按手势吗?

4

5 回答 5

12

以下是我使用 Swift 3 的方法:

protocol MyTabControllerProtocol: class {
    func tabLongPressed()
}

class MyTabController: UITabBarController {
    func viewDidLoad() {
        super.viewDidLoad()

        viewControllers = [
            // add your view controllers for each tab bar item
            // NOTE: if you want view controller to respond to long press, then it should extend MyTabControllerProtocol
        ]

        let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(astroButtonItemLongPressed(_:)))
        tabBar.addGestureRecognizer(longPressRecognizer)
    }

    func astroButtonItemLongPressed(_ recognizer: UILongPressGestureRecognizer) {
        guard recognizer.state == .began else { return }
        guard let tabBar = recognizer.view as? UITabBar else { return }
        guard let tabBarItems = tabBar.items else { return }
        guard let viewControllers = viewControllers else { return }
        guard tabBarItems.count == viewControllers.count else { return }

        let loc = recognizer.location(in: tabBar)

        for (index, item) in tabBarItems.enumerated() {
            guard let view = item.value(forKey: "view") as? UIView else { continue }
            guard view.frame.contains(loc) else { continue }

            if let nc = viewControllers[index] as? UINavigationController {
                if let vc = nc.viewControllers.first as? MyTabControllerProtocol {
                    vc.tabLongPressed()
                }
            } else if let vc = viewControllers[index] as? MyTabControllerProtocol {
                vc.tabLongPressed()
            }

            break
        }
    }
}
于 2017-09-13T20:11:58.050 回答
7

您可以子类化UITabBarController并添加一个UILongPressGestureRecognizer到它的tabBar. 作为手势识别器的代表,您可以选择何时检测到长按。由于一旦用户触摸标签栏项目就会被选中,您可以使用该selectedItem属性来执行此检查。

@interface TabBarController () <UIGestureRecognizerDelegate>
@property (nonatomic, strong) UILongPressGestureRecognizer *longPressRecognizer;

@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(recognizerFired:)];
    self.longPressRecognizer.delegate = self;
    [self.tabBar addGestureRecognizer:self.longPressRecognizer];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

    // This will ensure the long press only occurs for the
    // tab bar item which has it's tag set to 1.
    // You can set this in Interface Builder or in code
    // wherever you are creating your tabs.
    if (self.tabBar.selectedItem.tag == 1) {
        return YES;
    }
    else {
        return NO;
    }

}

- (void)recognizerFired:(UILongPressGestureRecognizer *)recognizer {
    // Handle the long press...
}

@end
于 2015-10-06T12:18:44.010 回答
2

我通过获取用户可以交互的特定 tabBarItem 视图并简单地添加长按手势来做到这一点。通过这种方式,您不必编写任何协议或子类化 TabBarViewController。

let longPressGestureRecognizer = UILongPressGestureRecognizer.init(target: self, action: #selector(longTap(_:)))
    longPressGestureRecognizer.minimumPressDuration = 1.0
    self.tabBarController?.orderedTabBarItemViews()[0].addGestureRecognizer(longPressGestureRecognizer)

至于获取 tabBarItemViews :

extension UITabBarController {
func orderedTabBarItemViews() -> [UIView] {
    let interactionViews = tabBar.subviews.filter({$0.isUserInteractionEnabled})
    return interactionViews.sorted(by: {$0.frame.minX < $1.frame.minX})
}

PS:viewController,即“self”是tabBarController的第一项。

于 2018-12-30T13:20:24.503 回答
2

这是 swift 5 中的一个解决方案:使用情节提要或代码将长按手势识别器添加到“整个”选项卡栏......并且不要忘记让您的 ViewController 成为它的代表......并实现下面的委托方法以检查传入的触摸是否位于标签栏子视图的“一个”内 .. 如果是则返回 true ,否则返回 false .. 以下是仅当我们长按第一个选项卡时才让识别器触发的代码:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {

    if touch.view?.isDescendant(of: tabBar.subviews[1]) == true {return true}
    return false



}

注意:tabbar.subviews 数组计数是项目数 + 1,这是标签栏的背景 .. 所以如果你想要第一个项目的视图,你可以找到它并且索引 1 而不是 0

于 2020-05-10T19:28:31.427 回答
1

如果你只需要识别一个 tabBar 项目的长按,你可以在相应的 viewController 的viewDidLoad方法中做到这一点:

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleLongPress:)];
[self.tabBarController.tabBar addGestureRecognizer: longPressGesture];

进而:

- (void)handleLongPress:(UILongPressGestureRecognizer *) recognizer {
    if (recognizer.state == UIGestureRecognizerStateBegan) {

        UITabBar *tabBar = ((UITabBar* )recognizer.view);

        if (tabBar.selectedItem == self.tabBarItem) {
            doSomethingVeryExciting();
        }
    }
}

如果你只是切换标签,这不会触发。

于 2017-01-09T20:48:57.100 回答