iphoneiosuibuttontouch-up-inside

BackgroundImage of UIButton sometimes did not change with touchUpInside event


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:)


Solution

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