1

The UIButton is set as following code:

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 27, 27)];
[btn setBackgroundImage:[UIImage imageNamed:@"imgUp.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"imgDown.png"] forState:UIControlStateHighlighted];
[btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];

If I touch the btw quickly, the imgDown.png will not appear but the action btnPressed: is fired. How could it be fixed? Any help is appreciated:)

lu yuan
  • 7,207
  • 9
  • 44
  • 78

4 Answers4

1

Add this line in your code:

[btn setBackgroundImage:[UIImage imageNamed:@"imgDown.png"] forState:UIControlStateSelected];
Adinia
  • 3,722
  • 5
  • 40
  • 58
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
0

You should call the setHiglighted: method for the button (btn) inside of btnPressed: function.

user1111278
  • 126
  • 4
0

The following code can solve the problem.

-(void)btnPressedDelay{

    //original code in btnPressed: method
}

- (void) btnPressed:(id)sender
{    

    [self performSelector:@selector(btnPressedDelay) withObject:nil afterDelay:0];
// 0 did the magic here

}
lu yuan
  • 7,207
  • 9
  • 44
  • 78
0

its working fine i will tested it pls check your images and try it :

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 27, 27)];
[button setBackgroundImage:[UIImage imageNamed:@"rainy.jpg"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"Icon.png"] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view button];
swalkner
  • 16,679
  • 31
  • 123
  • 210
Sameer
  • 1
  • If btnPressed: did nothing it may work fine. Here in mine code it will modal a viewcontroller. One solution is post by myself. – lu yuan Sep 27 '12 at 11:20