0

I have a custom view which I need to hide/unhide with animation. UIView animations are not working on hidden property.

So I have overridden setHidden where I modify the alpha with animation.

It works but while hiding it always seem to be slower than the rate at which it appears. I am giving 0.3 as duration but still disappearing happens slowly... and when it appears, its fast!

My Code

/* Will modify alpha instead of hidden var */
-(void)setHidden:(BOOL)hidden
{

    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = hidden?0.0:1.0;
    }];


}

/* need to override this so that, .hidden returns value based on alpha as we are not modifying the hidden ivar */
-(BOOL)isHidden
{

    return (self.alpha == 0.0);
}
Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135
  • This is not an answer for your question, but I just wanted to suggest instead of overriding the getter/setter of hidden and changing the alpha value within that, wouldn't it be simpler directly to change the alpha.. – Adithya Jun 12 '13 at 11:12
  • The thing is, hidden property is already being used at lot of places. and anyways I want it to happen with animation every time. – Amogh Talpallikar Jun 12 '13 at 11:13
  • Why don't you call `super -setHidden` after the animation is complete and return `super.hidden` at the getter ? This override looks bad as for me. – A-Live Jun 12 '13 at 11:15
  • I just did that in my code :) – Amogh Talpallikar Jun 12 '13 at 12:24
  • I found a better way to animate this using CATransition instead of animating alpha property and overriding both setter and getter. – Amogh Talpallikar Jun 12 '13 at 12:44

1 Answers1

0

Ok, So I used this instead.

/* Will modify alpha instead of hidden var */
-(void)setHidden:(BOOL)hidden
{
    CATransition *animation = [CATransition animation];
    animation.type = kCATransitionFade;
    animation.duration = 0.2;
    [self.layer addAnimation:animation forKey:nil];

    [super setHidden:hidden];
}
Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135