2

Update

The animation is working for setEnabled=NO.

The animation for setEnabled=YES is being triggered when UIScrollView is scrolling, the UIButton is inside the scrollview and the animation for setEnabled=NO is being triggered when UIScrollView is done scrolling.

So, I think the reason why animation for setEnabled=YES is not working is because the view is moving. I am not sure but this seems to be the only logical explanation from what I have found so far. I did a test with dispatch_after() and the animation worked for setEnabled too, in other words the animation is working if it is being triggered when the view is not moving.

What I need to do ?

I have two different background images for UIButton one for UIControlStateNormal and another for UIControlStateDisabled.

I want a effect where UIButton slowly transitions over from one state to another

What have I been doing ?

BOOL enableDisable = YES;
[UIView transitionWithView:((UIButton*)object)
                                          duration:3.3
                                           options:UIViewAnimationOptionTransitionCrossDissolve
                                        animations:^{ [((UIButton*)object) setEnabled:enableDisable]; }
                                        completion:nil];

The Problem

UIButton transforms to setEnabled=NO state over the duration but no matter what I put in the options setEnabled happens almost instantly.

is there something I am missing ?

Thanks in advance for your time and response.

Kishor Kundan
  • 3,135
  • 1
  • 23
  • 30

2 Answers2

4

Unfortunately, enable or disabled state for UIView aren't part of animatable properties in apple docs. The animatable properties are:

frame, center, bounds, transform, alpha, backgroundColor, contentStretch

Reference here: UIView animation

However if you want to create custom property for animation, you can have a look at this post which describes a way to achieve it. Create a custom animatable property

Community
  • 1
  • 1
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • `it is working for setEnabled = NO;` and this is what surprises me :( – Kishor Kundan Jan 06 '15 at 09:04
  • I agree with Adil, you should create your own UIControl or UIView, but use composition, do not subclass UIButton. – Andrea Jan 11 '15 at 14:29
  • Or just make your custom UIButton class, and create your own custom animation: something like `[button setEnabled:YES animated:YES]` – Vitalii Gozhenko Jan 15 '15 at 22:16
  • I am not sure animatable property are relevant here. We are talking about `transitionWithView:...` not `animateWithDuration:...` – sergio Jan 17 '15 at 18:03
0

I can confirm that your code works as expected both for the transition

enabled: NO -> YES

and for the transition

enabled: YES -> NO

So, my guess is that something else is happening in your app that somehow interferes with the transition. Try defining a completion block like:

...
completion:^(BOOL finished) {
    NSLog(@"FINISHED? %@", finished?@"YES":@"NO");
} ];

and add a trace log before the transitionWith call or inside the animation block to check how long the transition runs from start to completion.

My guess is that while the button is transitioning something else is happening that changes its state and as a secondary effect breaks the transition. I fear that without seeing more code, it will not be possible to help you further...

sergio
  • 68,819
  • 11
  • 102
  • 123
  • I am guessing the reason is the user interaction. Animation begins and stop instantly. I tried the same code with a explicit trigger and it worked perfect but when it is triggered from gesture it does not work – Kishor Kundan Jan 18 '15 at 17:25