简体   繁体   中英

How to disable a UIButton for 3 seconds?

Big edit: I'm a complete begginer to Obj-C and iOS development.

When my UIButton is pressed, a label with fade in. This label takes 3 seconds to do fade in. How would I go about disabling the UIButton for those 3 seconds?

This is what I was trying:

[UIView animateWithDuration:3.0f animations:^{
    self.button.enabled = NO;
    self.quoteLabel.alpha = 1.0f;
    self.quoteLabel.text = self.quotes.randomQuote;
    self.labelForNumberOfTimesRead.text = [NSString stringWithFormat:@"+ %d",numberOfTimesRead ];

}];
self.button.enabled = YES;

But it only disables for an 'instant'.

You can do it simply like this,

self.button.enabled = NO;
self.quoteLabel.text = self.quotes.randomQuote;
self.labelForNumberOfTimesRead.text = [NSString stringWithFormat:@"+ %d",numberOfTimesRead ];
    [UIView animateWithDuration:3 animations:^{
        self.quoteLabel.alpha = 1.0f;
        } completion:^(BOOL finished) {
            self.button.enabled = YES;
        }
         ];

It's not clear what you're doing with labelForNumberOfTimesRead. Do you want that one to fade in (or out) as well?

I think you should have put some code so we could help you better. For now I am adding psudocode .You can use NSTimer here .

  1. add [yourButton setEnable : NO] on the click of your button .

  2. And when your animation completes then add [yourButton setEnable : YES] .

  3. If you face any difficulty for getting the animation complete event then you can use the NSTimer also.

// Start by disabling the button
self.button.enabled = NO;

// This method will **queue** an animation but it won't start right away
[UIView animateWithDuration:3.0f animations:^{

    // Next animate anything you want
    self.quoteLabel.alpha = 1.0f;
    self.quoteLabel.text = self.quotes.randomQuote;
    self.labelForNumberOfTimesRead.text = [NSString stringWithFormat:@"+ %d",numberOfTimesRead ];

}
// Use completion block to re-enable button
completion:^(BOOL finished){
    self.button.enabled = YES;
}
];

// The following line is commented as it would be executed before the animation block!
// self.button.enabled = YES;

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