An iPhone button with a non-rectangular shape?

How can I get a non-rectangular button?

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

+5
source share
6 answers

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

I needed to do this not so long ago, and I did what Sixten Otto suggested. In my original question (UPDATE section) there are some tips for getting the alpha value for your image at a specific point:

?

: UIControl , - , "", UIButton PNG . , UIControl " ".

, UIControl , - , alpha == 0. drawRect: NSImage drawInRect:fromRec:operation:fraction:, .

:

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

: ( stateImage , , )

- (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];
}

png-. , 0:

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

, , . , !

N.B: Mac, iPhone, , .

+8

, , PNG, .

, , , . IPhone , , , , ​​ .

, , , , X/Y , , region ( , , 4 2 )

: , , , -. , , PNG - . .

+7

, . Apple, , , 44 .

UIButton UIButtonTypeCustom, PNG (. ). , , "" .

+2

.

+1

@Form 'drawRect' .

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

You can create your own button transparent for touch in some parts.

If you cannot use a custom image for a button (for example, if you draw text on a button), you can use the new method to draw a hierarchy of views in the image:

 - (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates;
+1
source

All Articles