简体   繁体   中英

Tap and hold: a pain in the uiBUTTon

Excuse the bad pun, I'm creating a custom tab bar in my iPhone app using UIButtons. Everything works beautifully except that when I tap and hold a button, it doesn't select it until I release it. It's really bugging me, because a standard UITabBarItem is selected on touch down and it just feels wrong.

I've set the IBAction to "Touch Down" and my code is pretty simple. Am I doing something wrong?

-(IBAction)tab1Pressed:(id)sender 
{
    if (self.tab1.selected == NO) {
        self.tab1.selected = YES;
        self.tab2.selected = NO;
    }
}

Got a solution! Added a UILongPressGestureRecognizer to each UIButton, instead of using IBActions. Worked like a charm!

In viewDidLoad:

tapAndHold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAndHold)];
[tapAndHold setMinimumPressDuration:0.01];
[self.myTabBarButton addGestureRecognizer:tapAndHold];
[tapAndHold release];

You should use another event for your button. The default one is "touch up inside". The action is triggered when you release the button.

Using "touch down" should do what you want.

您可以尝试子类化UIView并实现touchesBegan:withEvent:方法。

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