Problems with UIButton and UIControlEventState

I have a very specific “error” in the application for the iPhone. I set two images for the selected and normal button states. It works as expected when you “press” and then “touch” at a slow pace, but if you quickly press / tap it, notice the flicker between the states. Is this a known bug or is the state setting incorrect?

Here is the code that creates the buttons:

UIImage *normalImage = [[UIImage imageNamed:@"btn-small.png"] stretchableImageWithLeftCapWidth:10.0f topCapHeight:0.0f];
UIImage *highlightedImage = [[UIImage imageNamed:@"btn-small-down.png"] stretchableImageWithLeftCapWidth:10.0f topCapHeight:0.0f];

[self setBackgroundColor:[UIColor clearColor]];

[self setBackgroundImage:normalImage forState:UIControlStateNormal];
[self setBackgroundImage:highlightedImage forState:UIControlStateDisabled];
[self setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];

[self setAdjustsImageWhenDisabled:FALSE];
[self setAdjustsImageWhenHighlighted:FALSE];

When a button is pressed, it just turns itself off and turns on another button:

- (IBAction)aboutButtonTouched:(id)sender
{
    aboutButton.enabled = FALSE;
    rulesButton.enabled = TRUE;
}

- (IBAction)rulesButtonTouched:(id)sender
{
    rulesButton.enabled = FALSE;
    aboutButton.enabled = TRUE;
}

Any thoughts on this quick click?

+3
source share
3 answers

, . , , , , , , - .

, , aboutButtonTouched , NSUInteger:

- (IBAction)aboutButtonTouched:(id)sender
{
    rulesButton.enabled = TRUE;
    [sender setEnabled:FALSE];    

    NSLog(@"%d", [sender state]);
}

setEnabled, , "3". UIControlState:

enum {
   UIControlStateNormal               = 0,            // 0
   UIControlStateHighlighted          = 1 << 0,       // 1
   UIControlStateDisabled             = 1 << 1,       // 2
   UIControlStateSelected             = 1 << 2,       // 4
   UIControlStateApplication          = 0x00FF0000,
   UIControlStateReserved             = 0xFF000000
};

( , ). , "3" (0011) UIControlStateHighlighted | UIControlStateDisabled (0001 | 0010 1 | 2), , . , , ( " , , " - ). , , , :

[self setBackgroundImage:normalImage forState:UIControlStateNormal];
[self setBackgroundImage:highlightedImage forState:UIControlStateDisabled];
[self setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
[self setBackgroundImage:highlightedImage forState:UIControlStateHighlighted|UIControlStateDisabled];
+8

, , , , UIControlStateHighlighted|UIControlStateDisabled, : UIControlStateDisabled.

:

(UIControlStateHighlighted | UIControlStateDisabled) == UIControlStateHighlighted

(UIControlStateSelected | UIControlStateHighlighted | UIControlStateDisabled) == (UIControlStateSelected | UIControlStateHighlighted)

, : Highlighted + Disabled, Highlighted. , , ...

+1

Maybe you should change the button change sequence

- (IBAction)aboutButtonTouched:(id)sender
{
    rulesButton.enabled = TRUE;
    aboutButton.enabled = FALSE;
}

When you first hide one button and then show the other, there may be a small space that creates a flicker. I think it's better to show another button first.

0
source

Source: https://habr.com/ru/post/1739784/


All Articles