views:

74

answers:

3

What would be the way to draw a triangular button on the iPhone. It would be nice if I could also change its parameters (backgroundcolor, font etc..) as with the regular buttons.

Ideally I would like equilateral triangles.

A: 

You could transform you button/view using core animation 3d transform. I am not sure how it will look like inside of it. but you surely can get a triangle form view. You can apply a rotation in 3D (arround X or Y axis, not Z) with a large enough angle that it looks like a triangle.

An alternative approach is to make it look like triangular. You probably not need it TO BE triangular. So, maybe by setting a triangular look like image or maybe by hiding part of the button with (2D) rotated view or layer.

nacho4d
Actually I need it to BE triangular
John Smith
A: 

If you are not changing too much the background images of your buttons, I suggest to put in the resources, some .png file you create using photoshop for example, in which the triangular part has full opacity and the remaining areas are full transparent.

Then create UIImage using the method : [UIImage imageNamed:file_path_inside_resources_folder] and set it as background image for your button.

Note that you need to do the work only once, since the UIButton will resize the picture according to its own frame dimensions.

Regards Meir Assayag

Meir Assayag
The only possibility I have now is lots of png's because the colors change often. It's what I;m trying to avoid, because it drastically slows my development, and is horribly inefficient
John Smith
Another suggestion: add a subview to your UIButton.This subview may have some member parameters like color, line width etc..Then on drawRect function, draw your triangle with these parameters.You can use the CGContext graphics function for this task.The refresh function will be called using [yourSubView setNeedsDisplay] for example on key down/up.To alternate between colors, only change the parameter before calling the setNeedsDisplay function.
Meir Assayag