简体   繁体   中英

UIButton in UIToolbar not responding after alpha change

I've got a project created using storyboards. I have a UIToolbar , in it a UIToolarItem and in that a UIButton .

When the user taps on the screen it triggers an action that will animate the alpha value of the UIButton

as below: (this is the method being called to change the alpha of the button

- (IBAction)tapDetected:(UIGestureRecognizer *)sender {
    NSLog(@"tapped: %f", self.mainViewToolbar.alpha);
    NSLog(@"%s", __PRETTY_FUNCTION__);


    if (self.buttonCall.alpha > 0.0) {
        [UIView animateWithDuration:1.0 animations:^(void) {
            self.buttonCall.alpha = 0.0;
        }];
    }
    else //(self.buttonEmergencyCall.alpha < 1.0)
    {
        [UIView animateWithDuration:1.0
                              delay: 0.0
                            options: UIViewAnimationOptionTransitionFlipFromLeft
                         animations:^{
                             self.buttonCall.alpha = 1.0;
                         }
                         completion:nil];

        [UIView animateWithDuration:1.0
                              delay: 5.0
                            options: UIViewAnimationOptionTransitionFlipFromLeft
                         animations:^{
                             self.buttonCall.alpha = 0.0;
                         }
                         completion:nil];
    }


    if (sender.view == self.toolbarForMainScreen) {
        NSLog(@"clicking on the toolbar");
    }

}

The button response to clicks until the code above is run, then it stops responding to user taps.

Any ideas?

you are setting alpha is 0 that you controller going to be hide.you must set minimum alpha is 0.1 for getting it's event else button going to hide. See this i test with demo that i setting one IBAction with click touch up inside Method:

 float alf = 1.0;
- (IBAction)addSubview:(id)sender
{
    [self.btnAdd setAlpha:alf];

    alf-=0.1;
    NSLog(@"Tapp working");

}

and each click of Button i decrease 0.1 from alpha 1 and it method called till that button alpha is 0.1 then not calling. So it means that when you setting alpha is 0 that controller going to be hide. At list you have to set minimum Alpha of control is 0.1

在此处输入图片说明

看看是否有其他视图覆盖在UIbutton上,然后

Best Practice: Try using completion block like this

[UIView animateWithDuration:1.0 animations:^{

    self.buttonCall.alpha = 1.0;        

} completion:^(BOOL finished) {

    [UIView animateWithDuration:1.0 animations:^{

        self.buttonCall.alpha = 0.0;

    } completion:^(BOOL finished) {


    }];
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM