非矩形形状的 iPhone 按钮? [英] iPhone button with non-rectangle shape?

查看:24
本文介绍了非矩形形状的 iPhone 按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得非矩形形状的按钮?

How can I get a non-rectangular shaped button?

例如,我确实有一个具有 alpha 透明度的 PNG 图像.如何在没有透明颜色的情况下将按钮的形状设置为此图像.

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.

推荐答案

就像其他人说的,你应该有一个合理的界面来注册触摸事件,但我相信你知道这一点并有你的理由,所以我只会告诉你你是怎么做到的:

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:

不久前我需要这样做,而我所做的正是 Sixten Otto 刚刚建议的.我的原始问题(更新"部分)中有一些提示可用于在某个点获取图像的 alpha 值:

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:

如何用非矩形创建透明窗口矩形按钮?

我建议在下面的示例中对 UIControl 进行子类化,但是如果除了非矩形"之外,您不需要按钮上的任何特殊行为,那么只需对无边框 UIButton 集进行子类化使用您的 PNG 将完成这项工作并且需要更少的工作.您可以通过继承 UIControl 并以困难的方式"实现对控件行为的更多控制.

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.

我建议您子类化 UIControl 并覆盖触摸事件方法,然后检查点击点下的 alpha,如果 alpha == 0 则不处理该事件.对于绘图,您覆盖 drawRect: 并使用 NSImagedrawInRect:fromRec:operation:fraction: 方法在那里绘制图像控件的框架.

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

然后你可以将它绘制到控件的视图中:(其中 stateImage 是指向必须根据按钮是否按下而绘制的图像的指针)

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

此时您已经用 png 图像绘制了按钮.如果 alpha 不是 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!

注意:我的示例适用于 Mac,但它也应该适用于 iPhone,可能需要稍作修改.

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

这篇关于非矩形形状的 iPhone 按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆