Question

I have problems touching a custom button multiple times in a row if I animate it in the setHighlighted: method. Basically I want to change the background color of the button on touch.

-(void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];

    if(highlighted)
    {
        [UIView animateWithDuration:0.1
                delay:0.0
                options:UIViewAnimationOptionAllowUserInteraction
                animations:^{
                    self.backgroundColor = [UIColor darkGrayColor];
        } completion:^(BOOL finished) {}];
    }
    else
    {
        [UIView animateWithDuration:0.1 animations:^{
            self.backgroundColor = [UIColor clearColor];
        } completion:^(BOOL finished) {}];
    }

The Problem is that I have to wait until the animations finish. During the animation, the target action of the button isn't called. How can I handle these multiple touches?

Was it helpful?

Solution

Try adding delay:0.0 options:UIViewAnimationOptionAllowUserInteraction to your other animateWithDuration: block. Or, you can simplify your code by combining both animation blocks like this:

Code:

-(void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];

    UIColor *colorToChangeTo = highlighted ? [UIColor darkGrayColor] : [UIColor clearColor];

    [UIView animateWithDuration:0.1
                          delay:0.0
                        options:UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         self.backgroundColor = colorToChangeTo;
                     } completion:^(BOOL finished) {}];
}

OTHER TIPS

your .h file bool isCan;

.m file

 - (void)viewDidLoad
    {
        isCan=YES;
        [super viewDidLoad];
        NSArray *temp=[NSArray arrayWithObjects:@"button1",nil];
        for (int i=0; i<=0; i++)
        {
            _likeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
            [_likeBtn setFrame:CGRectMake(20+(i*80), 220, 102, 20 )];
            [_likeBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            [_likeBtn setTitle:[temp objectAtIndex:i] forState:UIControlStateNormal];
            [_likeBtn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
            [_likeBtn setTag:i];
            [self.view addSubview:_likeBtn];
        }

        // Do any additional setup after loading the view from its nib.
    }

    -(void)setHighlighted:(BOOL)highlighted
    {

        if(highlighted)
        {
            [UIView animateWithDuration:0.1
                                  delay:0.0
                                options:UIViewAnimationOptionAllowUserInteraction
                             animations:^{
                                 self.likeBtn.backgroundColor = [UIColor blackColor];
                             } completion:^(BOOL finished) {}];
        }
        else
        {
            [UIView animateWithDuration:0.1 animations:^{
                self.likeBtn.backgroundColor = [UIColor redColor];
            } completion:^(BOOL finished) {}];
        }
    }
    - (IBAction)action:(id)sender

    {
        if (isCan)
        {
            [self setHighlighted:YES];
            isCan=NO;
        }
    else
    {
        [self setHighlighted:NO];
isCan=YES;
    }
    }

You can just set a dark grey colored image as the button highlighted image.

Update

Although I would still recommend going with the highlighted image, here is an alternate solution.

- (void)setHighlighted:(BOOL)highlighted
{
    [UIView transitionWithView:self
                      duration:0.3f
                       options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowUserInteraction
                    animations:^{
                        self.backgroundColor = highlighted ? [UIColor darkGrayColor] : [UIColor clearColor];
                        [super setHighlighted:highlighted];
                    } completion:nil];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top