我可以在UITabbarController中处理双击

时间:2014-11-13 16:37:00

标签: ios objective-c uitabbarcontroller uitabbaritem

我需要处理双击操作以从使用自定义视图表示的导航路径返回。

通常双击会解除导航控制器堆栈中嵌套的最顶层控制器。我想处理这个动作并做点别的事。

将代码放入(BOOL)tabBarController:shouldSelectViewController:没有帮助,因为单击和双击之间没有区别。

感谢。

4 个答案:

答案 0 :(得分:3)

我在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
}

答案 1 :(得分:2)

我必须在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 
        }

这没有任何私人api调用很好地工作。但我想知道是否有更好,更简单的方式。

答案 2 :(得分:0)

试试这个:

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

答案 3 :(得分:-1)

我相信你甚至需要使用触摸......

检查下面类似的堆栈溢出:

Objective-c: How to detect double tap on view?