Wednesday, May 19, 2010

Improved Gradient Buttons

I've been playing around a bit, improving my imageless gradient button class. The new version allows you to specify the gradient for the normal and highlighted state by populating two arrays, one with the colors that make up the gradients and another with the relative location for each color. I've gotten rid of the abstract parent class and individual child classes and all the functionality is now contained in a single class.

Screen shot 2010-05-19 at 11.48.50 PM.png
There are five built-in styles which can be seen in the image above, or you can manually set the gradient to any value you'd like. You can download the source codes from the Google code page. There are no restrictions or limitations on its use.

The easiest way to use these is to add a UIButton instance to your view in Interface Builder, then change the underlying class from UIButton to GradientButton. Because there's no way to create IB palettes for iPhone classes, you'll also have to implement viewDidLoad and set the gradient or use the existing methods there.



24 comments:

Johann.Fradj said...

A suugestion :
Instead of use that "self.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0];"
use that "self.backgroundColor = [UIColor clearColor];"
Is just more readable ;-)

Johann.Fradj said...

Also, instead of
"color = [UIColor colorWithRed:0.076 green:0.103 blue:0.195 alpha:1.0];
[color setStroke];"

CGContextSetRGBStrokeColor(context, 0.076, 0.103, 0.195, 1.0);
It prevents at least 2 unecessary calls and an object (the color) creation.

I have not read your code in depth yet, but if you want I can send you my "version" ?

Regards,

Jeff LaMarche said...

Johann:

I haven't taken the opportunity to scrub the CG code yet - most of it not actually my code, it's machine generated code from Opacity.app.

If you feel like cleaning it up, I'm more than happy to make you a committer on the project.

Johann.Fradj said...

I did not realize that the CG Code was produced by Opacity...

If I find a little time (I really need to stop sleeping ;-), I'll make you a cleaner version ;-).

Maybe I should forward my improvements to the Opacity creator.

See you soon.

Regards,

My store said...

The best home based alife shoes sale business to start is one that alife sneakers sale new york gives you the ability to alife sneakers for sale leverage your time. Basically, you want a cheap alife sneakers business that continues to operate alife sneakers and earn income for you even while you’re not working. cheap alife shoes sale This is key if you want to spend more time with alife everybody hi your family. Businesses that lack alife everybody low this feature end up owning you, alife shoes online sale and you’ll most likely have less free time than you did as a 9 to 5 employee.

My store said...

Introduced in 1966, Onitsuka Tiger Mexico 66 were the first shoes with the famous Tiger Stripes. ASICS GEL-KINSEI 2 were worn at the Mexico Olympic Games in 1968. With lace up asics gel kinsei 2 sale front and tracks in asics gel kinsei 2 mens the side and suede asics kinsei 2 for sale effect trim on front and back. Purchase Asics Onitsuka Tiger mini cooper and receive onitsuka tiger mini clubman Learn about running shoe types such as motion control, onitsuka tiger mid runner performance training and stability onitsuka tiger mexico 66 mens shoes and discover which type authentic onitsuka tiger shoes sale best fits your pronation type.The original Kinsei was asics gel stratus mens shoes classified by Asics as a asics gel stratus 2.1 for mens Stability+ shoe. The Kinsei II is asics gel stratus 2.1 sale Neutral+ shoe. If you look at the Asics Shoe Fit Chart

My store said...

These shoes made my trip to onitsuka tiger mexico 66 yellow black Argentina that much more enjoyable. I needed a pair of onitsuka tiger mexico 66 by asics shoes that I could walk great mens onitsuka tiger mexico 66 distances in and stand in for asics onitsuka tiger mexico 66 several hours at a time. These were my answer. They have great support, and give the feeling asics tiger mexico 66 that one is walking on air. They did get dirty, when we went rappelling. onitsuka tiger mini clubman Although you are not supposed to, I washed them on gently cycle in my front loader onituka tiger mini cooper washing machine. No harm, no foul. I highly recommend them.

My store said...

They are your New Balance for sale , so here at New Balance Altoona we know new balance 580 for sale that each athlete has different needs, new balance 574 and different feet. We strive to new balance 996 provide the right shoe and a perfect fit for new balance 993 every customer. All athletes understand the new balance 1500 connection between superior new balance 1300 performance and well-fitted shoes - new balance 1400 that's why we carry New Balance 576 sneakers and footwear in a wide new balance 577 variety of sizes and widths so that we can provide the new balance 999 best fit for the best price.

Mikey said...

When putting one of these buttons into a UITableView's footer, tapping the button causes it to appear to remain in the 'down' position until its superview is popped/released. How can I forcibly reset the button appearance to the UIControlStateNormal look?

Mike said...

Doesn't work for me under iphone 4 sdk...

Mike said...

To clarify, not all of them work for me in the 4th sdk. The red and green ones don't work, the alertstyle one does.

David W Sica said...

Anyone have a fix for Mikey's bug (from 6/12) that I am also encountering?

Michael said...

I have the same issue as Mikey. Even with v0.2

Jeff have you any thoughts on this?

Michael said...

Mikey, I found a solution.

To ensure that the button goes back to it's normal state after a click touch, I needed to increase the delay to 0.2

i.e. from

[self performSelector:@selector(hesitateUpdate) withObject:nil afterDelay:0.1];

to this:

[self performSelector:@selector(hesitateUpdate) withObject:nil afterDelay:0.2];

No guarantee this won't break in future but seems to work for iOS 4.0

Marika said...

Hey,

I am using GradientButton for my buttons, which look good if I create them in the Interface Builder. But if I add buttons programmatically, it looks creepy. (Well it's not too bad, but it's not as nice as it usually is).
Thats how I do create the buttons:

GradientButton *button = [GradientButton buttonWithType:UIButtonTypeCustom];
[button useRedDeleteStyle];

button.frame = CGRectMake(212, 8, 100, 30);

[button setTitle:@"Click Me!" forState:UIControlStateNormal];

[self.view addSubview:button];

I assume, that it has something to do with the UIButtonType as here it is Custom while the Interface Builder uses RoundedRect. But I don't know how to use it the correct way (always crashes).

I guess I am not the first to add buttons programmatically, so I would be thankful for any indication what I am doing wrong.

Regards,

Sources said...

great shoes! love the style and design of them.
-------
Free Apple Ipad 4

King Bayern Munich said...

Cool. Talking about makes sense. Is recognized. Hope that it will often share such a good text. I will always be concerned about, because you can learn some knowledge, thank you for sharing, and i love Cold Rolling Machine

SEO Services Consultants said...

Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck! Web Design Company

UFAM said...

Hi!

I am new on iOS development, and have your books on iPhone3 devel serie, so I was thankful when I saw this post about changing the color of a button.
I tried used to change the color os a gradient button after some computation had finished. I started the color with

[button useWhiteActionSheetStyle]

and then, after some computation I tried

[button useGreenConfirmStyle]

I want the effect of changing the color to mean that
some task was fulfilled.

But only the text changed to white, but not the
button. What could be done to solve this?

Many thanks in advance!
BR

UFAM said...

This google account setting is crazy today. The UFAM
post was me (Edjard).

Thanks!
Edjard

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels

p90xexercise said...

Thanks for this. Iphone is really improving by leaps and bounds.

Hizon said...

no doubt apple became popular. I believe it's because of their very splendid products. I also like their applications and their units too.

Sasha of skin tag removal

KiltedGreen said...

Thanks very much Jeff - very handy.