Yossi
Yossi

Reputation: 2535

UIButton is not highlighted when pushing to new view

I have a UIButton that should change the background image when it been selected. When there was no activity to the button, or when the activity is inside the same UIViewController, all works fine.

But if the button is activating a push to another view, then the user will never see the image which should appears when he press on the button.

I can fix that with using [NSTimer scheduledTimerWithTimeInterval:0.1 ....

but I am wondering if there is a way to do it without any delay.

Code:

playBTN = [[UIButton alloc] initWithFrame:CGRectMake(100, 90, 121, 119)];
[playBTN setImage:[UIImage imageNamed:@"play_BTN"] forState:UIControlStateNormal];
[playBTN setImage:[UIImage imageNamed:@"play_BTN_press"] forState:UIControlStateHighlighted];
[playBTN setImage:[UIImage imageNamed:@"play_BTN_press"] forState:UIControlStateSelected];
[playBTN addTarget:self action:@selector(actPlay) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:playBTN];


-(void)actPlay
{
    GamePlayViewController *game = [[GamePlayViewController alloc] initWithNibName:@"GamePlayViewController" bundle:nil];
    [self.navigationController pushViewController:game animated:YES];
}

Upvotes: 0

Views: 89

Answers (1)

Duncan C
Duncan C

Reputation: 131436

Normally buttons are connected to the UIControlEventTouchUpInside event on a button. That means that the button highlights when the user touches it, and the user can move his/her finger outside the button and release without triggering it if they touch it by mistake. It also means that the touch animation happens before the action method fires.

I suggest connecting your action to the UIControlEventTouchUpInside event

Upvotes: 2

Related Questions