Can I handle double tap in UITabbarController
I need to handle a double tap action to get back out of the navigation path presented with a custom view.
Typically a double click will reject the nested top controller in the navigation controller stack. I would like to handle this action and do something else.
Placing the code in (BOOL) tabBarController: shouldSelectViewController: doesn't help as there is no difference between single and double click.
Thank.
+3
source to share
4 answers
I had to add tapCounter to tabBarController: shouldSelectViewController: implementation:
self.tapCounter++;
// rule out possibility when the user taps on two different tabs very fast
BOOL hasTappedTwiceOnOneTab = NO;
if(self.previousHandledViewController == viewController) {
hasTappedTwiceOnOneTab = YES;
}
self.previousHandledViewController = viewController;
// this code is called in the case when the user tapped twice faster then tapTimeRange
CGFloat tapTimeRange = 0.3;
if(self.tapCounter == 2 && hasTappedTwiceOnOneTab) {
// do something when tapped twice
self.tapCounter = 0;
return NO; // or YES when you want the default engine process the event
} else if(self.tapCounter == 1) {
__block BOOL isSameViewControllerSelected = self.selectedViewController == viewController;
if(isSameViewControllerSelected) {
// do something when tapped once
}
dispatch_after_delay_on_main_queue(tapTimeRange, ^{
self.tapCounter = 0; // reset the counter in case there is a single tap followed with another one, but with longer time then tapTimeRange
});
return NO; // or YES when you want the default engine process the event
}
This works well without any private api calls. But I would like to know if there is a better and less complicated way.
+2
source to share
I am rewriting the Slovak Slovak answer in Swift 2 in case anyone needs it
var tapCounter : Int = 0
var previousVC = UIViewController()
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
self.tapCounter++
let hasTappedTwice = self.previousVC == viewController
self.previousVC = viewController
if self.tapCounter == 2 && hasTappedTwice {
self.tapCounter = 0
print ("Double Tapped!")
}
if self.tapCounter == 1 {
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.3 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue(), {
self.tapCounter = 0
})
}
return true
}
+1
source to share
try this:
@property (nonatomic, assign) NSTimeInterval tapTime;
@property (nonatomic, weak) UIViewController *prevVC;
- viewDidLoad {
self.tabBarController.delegate = self;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
NSTimeInterval duration = currentTime - self.tapTime;
self.tapTime = currentTime;
if (viewController == self.prevVC) {
if (duration < 0.35) {
// double tap detected! write your code here
self.tapTime = 0;
}
}
self.prevVC = viewController;
}
0
source to share