iPhone button with non-rectangle shape?

How can I get a non-rectangular shaped button?

For example I do have a PNG image with alpha transparency. How can I set the shape of a button to this image without the transparent color.

13.10.2009 17:02:44
More details on exactly what it is you're looking to achieve may get you where you want to go faster.
fbrereto 13.10.2009 17:07:32

Like the others say you should have a reasonable surface for registering touch events, but I'm sure you know that and have your reasons so I'll just tell you how I did it:

I needed to do this not so long ago and what I did is what Sixten Otto just suggested. There are some hints in my original question ("UPDATE" section) for getting the alpha value for your image at a certain point:

How to create a transparent window with non-rectangular buttons?

Edit: I suggest subclassing UIControl in the example below but if you don't need any special behavior on your button apart from the "non-rectangleness" of it then just subclassing a borderless UIButton set up with your PNG will do the job and require less work. You have more control on the control's behavior by subclassing UIControl and doing it the "hard way" though.

I would suggest that you subclass UIControl and override the touch event methods, then check for alpha under the tapped point and not handle the event if alpha == 0. For drawing you override drawRect: and use the NSImage's drawInRect:fromRec:operation:fraction: method in there to draw your image in the control's frame.

First you need to load the image and get a bitmap representation of it:

buttonImage = [NSImage imageNamed:@"myButtonImage"];
buttonImageRep = [[buttonImage representations] objectAtIndex:0];

Then you can draw it to the control's view: (where stateImage is a pointer to the image that must be drawn depending on if the button is pressed or not)

- (void)drawRect:(NSRect)aRect {
 [stateImage drawInRect:[self bounds] fromRect:NSMakeRect(0.0,0.0,[buttonImage size].width,[buttonImage size].height)
      operation:NSCompositeSourceOver fraction:1.0];

At this point you have drawn your button with your png image. You can override the touch event methods and handle the event if the alpha is not 0:

NSColor *colorUnderMouse = [buttonImageRep colorAtX:mouse.x y:mouse.y];
float alpha = [colorUnderMouse alphaComponent];

That's what I did and it works wonderfully. Hope this helps!

N.B: My example is for the Mac but it should also work on the iPhone, maybe with some slight modifications.

23.05.2017 12:02:59

Rectangular buttons are a button type, you can use the custom button type and assign an image to get rid of the rectangular edges of the button...heres a reference to button types http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/doc/c_ref/UIButtonType and to UIButton http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html

13.10.2009 17:28:04

It sounds like you are looking to have the clickable area of the button exactly match the PNG you are using.

If that is what you are looking for, I would firstly say to not do that. The iPhone is pressed using a finger, which generally doesn't have the accuracy to distinguish such a small region.

However if you are stuck on the idea, then the solution is to not use buttons at all, instead handle the click in a parent frame and manually interpret the X/Y value of the click to determine if it is in some bounding region (In the case of a rounded edge button, would likely consist of oring the result of checking 4 circles and 2 rectangles)

Edit: Realizing part of your original question, I noticed you mentioned you wanted to handle the function automatically based on the Alpha channel. While I would recommend my method of bounding regions, you could in theory accomplish this by sampling the PNG to test the Alpha channel at a value offset by the origin of the button. Potentially even doing this in a normal button's click event.

13.10.2009 17:52:21

As Guvante says, it's really not a good idea to rely on precision touches. Apple, for instance, recommends that controls be at least 44px across.

I'd recommend using a UIButton of type UIButtonTypeCustom, set the button's image to your PNG, and register for touch events (see here for more on event handling). Then, in your action method, you can get the coordinates of the touch out of the event, and test those coordinates against the alpha of your image to see whether you should treat it as a "real" touch or not.

23.05.2017 10:33:01

Another option could be to split your button up into multiple, rectangular parts.

21.10.2009 21:36:05

@Form right about using 'drawRect' to implement custom shape for button.

But better method to exam touch inside or not inside button is using of

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;

You can create your button transparent for touches in some parts.

If you can't use custom image for button (for example if you draw text on the button) you can use new method to draw subviews hierarchy into a image:

 - (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates;
22.11.2016 06:25:47