UIButton adding color with animation
Asked Answered
A

2

6

What i want to do is, when the user clicks the button then the button should fill with color with animation from bottom to top.

I tried this which added the color but the animation effect was not added.

float originalY = btn.frame.origin.y;
float originalH = btn.bounds.size.height;

[UIView animateWithDuration:3.0f
                      delay:1.0f
                    options:UIViewAnimationOptionTransitionFlipFromBottom
                 animations:^{

    btn.frame = CGRectMake(btn.frame.origin.x, (originalY + originalH), btn.bounds.size.width, 0);
    [btn setBackgroundImage:[UIImage imageNamed:@"Screen Shot 2012-11-07 at 4.22.30 PM.png"] forState:UIControlStateNormal];
    [btn setTitleColor:[[UIColor alloc]initWithRed:38.0/255.0 green:38.0/255.0 blue:38.0/255.0 alpha:1.0] forState:UIControlStateNormal];
} completion:^(BOOL finished) {
}];
Adalineadall answered 7/11, 2012 at 13:24 Comment(2)
Do you have set the color before the animation block?Suffering
No i have set the color in the animation block only. It gets set, just the animation is not coming.Adalineadall
C
-1

The best solution would be to subclass UIButton and add a method that changes the background image's alpha value from 0 to 1.

Or you could just override the drawRect method and fill up the background with a color there.

Curarize answered 8/11, 2012 at 19:58 Comment(0)
U
-2

i think you probably do something like this, i used UIlabel it may also work for UIButton to .

#import <QuartzCore/QuartzCore.h>

....

'theLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;

[UIView animateWithDuration:2.0 animations:^{ theLabel.layer.backgroundColor = [UIColor greenColor].CGColor; } completion:NULL];'`

Understood answered 12/8, 2014 at 10:46 Comment(2)
That is not going to change color bottom to top - it may however change to color of the whole button but I don't think this is the question...Vincenzovincible
increase the time in animateWithDuration: and it will show the effect thanks.Understood

© 2022 - 2024 — McMap. All rights reserved.