Omar Sinan
Omar Sinan

Reputation: 117

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: enter image description here

Any other suggestions?

Upvotes: 1

Views: 1838

Answers (3)

Waseem Sarwar
Waseem Sarwar

Reputation: 2725

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)
}

Upvotes: 0

JiuJitsuCoder
JiuJitsuCoder

Reputation: 1876

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.

Upvotes: 1

Welton122
Welton122

Reputation: 1131

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.

Upvotes: 2

Related Questions