Animate the enabling of a UIBarButtonItem?
Is there a way to animate enabling or disabling a button? I've tried the following with no success. I'm guessing at this point that the enabled property cannot be animated like opacity can – but I hope I'm wrong.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
theButton.enabled = YES;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
开发者_StackOverflow
I can't believe there isn't a setEnabled:(BOOL)enabled animated:(BOOL)animated
method.
This is actually really easy. UIView.transitionWithView
can take a snapshot of any view and cross-dissolve from the snapshot to the new state of the view. You can use that capability to animate a lot of changes, including enabled
.
Just use the View Controller's view as the target view, specify TransitionCrossDissolve
, then animate as you would normally.
UIView.transitionWithView(self.view,
duration: 0.5,
options: UIViewAnimationOptions.TransitionCrossDissolve,
animations: { () -> Void in
self.someButton.enabled = !self.someButton.enabled
}, completion:nil)
Animatable properties (through UIView beginAnimations:context:) are: frame, bounds, center, transform, alpha (from Apple docs).
So you've to determine what exactly your animation is (in terms of color/alpha/transform animations) and create manual animation (maybe you'll have to use CAAnimation instead of UIView beginAnimations:context:)
You can break down the enable/disable of a button into a change in opacity between two overlapping images each in one state or the other (i.e. make it look like a fade-in/fade-out). In the animation completion handler you can to do the actual enable/disable toggle.
Here's an idea :)
Within your animation block, Remove the button from view and add it again with disabled state. You can specify what kinda animation you want here..
Set: self.buttonSetEvent.customView.alpha = 0.2;
and try this
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.15];
[UIView setAnimationRepeatCount:2];
[UIView setAnimationRepeatAutoreverses:YES];
self.buttonSetEvent.customView.alpha = 1;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
精彩评论