简体   繁体   中英

How can I detect a double tap on UITabBarItem?

I've tried adding a gesture recogniser, not realising that it isn't possible with a UITabBarItem, here is what I did: 在此处输入图片说明

Any other suggestions?

I don't think this will have a simple solution, as double tapping a tab bar item is not expected behavior. Even if you got it working, I would assume Apple would not pass the review, as it will likely go against their design guidelines.

You'll see that error whenever you try to add a gesture recognizer to an object that doesn't inherit from UIView. Try adding the gesture recognizer to the UITabBar and then using the point where the double tap occurred, matching it to the correct tab bar item.

One simple solution can be like this.

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {

    let name = viewController.tabBarItem.title ?? ""
    if(name == "Home"){

        if(name == self.selectedTabName){
            print("tapped home again")
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "scrollToTop"), object: nil)
        }
    }
    self.selectedTabName = name
    self.delay(0.5){

        self.selectedTabName = ""
    }

}
 func delay(_ delay:Double, closure:@escaping ()->()) {

    let when = DispatchTime.now() + delay
    DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM