简体   繁体   English

我可以在UITabbarController中处理双击

[英]Can I handle the double tap in UITabbarController

I need to handle the double tap action to get back from a navigation path represented with a custom view. 我需要处理双击操作以从使用自定义视图表示的导航路径返回。

Usually the double tap dismisses the nested topmost controller in the navigation controller's stack. 通常,双击会解除导航控制器堆栈中嵌套的最顶层控制器。 I'd like to handle this action and do something else. 我想处理这个动作并做其他事情。

Placing code in (BOOL)tabBarController:shouldSelectViewController: does not help as there is no difference between the single and double tap. 将代码放入(BOOL)tabBarController:shouldSelectViewController:没有帮助,因为单击和双击之间没有区别。

Thanks. 谢谢。

I rewrite Vladimír Slovak's answer in Swift 2, in case anyone need it 我在Swift 2中重写了Vladimír斯洛伐克的答案,万一有人需要它

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
}

I had to add a tapCounter in the tabBarController:shouldSelectViewController: implementation: 我必须在tabBarController中添加一个tapCounter: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 nicely without any private api calls. 这没有任何私人api调用很好地工作。 But I'd like to know if there is a better and a less complicated way. 但我想知道是否有更好,更简单的方式。

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

I believe you need to use a touch even... 我相信你甚至需要使用触摸......

check out a similar stack overflow below: 看看下面类似的堆栈溢出:

Objective-c: How to detect double tap on view? Objective-c:如何在视图上检测双击?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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