nathan
nathan

Reputation: 1506

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

Upvotes: 0

Views: 1184

Answers (3)

nathan
nathan

Reputation: 1506

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

Upvotes: 3

Zoleas
Zoleas

Reputation: 4879

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.

Upvotes: 2

Sepehr Mahmoudian
Sepehr Mahmoudian

Reputation: 771

You can try subclassing a UIView and implementing touchesBegan:withEvent: method.

Upvotes: 1

Related Questions