BackgroundImage of UIButton sometimes did not change with touchUpInside event
-
04-07-2021 - |
質問
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:)
解決 3
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
}
他のヒント
Add this line in your code:
[btn setBackgroundImage:[UIImage imageNamed:@"imgDown.png"] forState:UIControlStateSelected];
You should call the setHiglighted: method for the button (btn) inside of btnPressed: function.
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];
所属していません StackOverflow