iphone - 如何扩展按键触控区域? [英] iphone - how to extend a button touch area?

查看:105
本文介绍了iphone - 如何扩展按键触控区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我连续有5个圆形按钮,每个按钮40x40像素。在每一个之间,我有20个像素。

I have 5 round buttons in a row, each one, 40x40 pixels. Between each one, I have 20 pixels.

40x40像素太小而无法触摸,但由于每个按钮之间有20个像素的空间我可以扩展按钮触摸区域至60x60像素,易于触摸。我可以简单地使用创建方形60x60像素透明图像的脏解决方案,将其放在按钮上并使其可触摸,但我知道可以通过创建自定义类并更改参数来扩展按钮触摸区域。

40x40 pixels is too small to touch, but as I have 20 pixels of space between each button I can extend the button touch area to 60x60 pixels, making it easy to touch. I could simply using the dirty solution of creating a square 60x60 pixels transparent image, put this over the button and make this touchable, but I know it is possible to extend a button touch area by creating a custom class and changing a parameter.

我知道这是可能的,因为我之前看到过这个(但我找不到网址)。我知道它与hitTest有关。

I know this is possible because I saw this done before (but I cannot find the URL). I know it is something related to hitTest.

如何做到这一点?
谢谢。

How can this be done? Thanks.

推荐答案

Eiko是绝对正确的。这里有一些你可以使用的简单代码,它独立于某个人给我扩展信息按钮框的位置。

Eiko is absolutely right. Here's some simple code you can use that is independent of the button's location that somebody gave me for expanding the Info button frame.

CGRect newInfoButtonRect = CGRectMake(infoButton.frame.origin.x - 25, 
                                          infoButton.frame.origin.y - 25, 
                                          infoButton.frame.size.width + 50, 
                                          infoButton.frame.size.height + 50);
    [infoButton setFrame:newInfoButtonRect];

您可能需要注意是否使用背景图像而不是图像(按钮setBackgroundImage: vs 按钮setImage:forState:)因为背景图像会随帧一起拉伸而正常图像不会。

You might want to watch out if you're using a background image as opposed to an image ( button setBackgroundImage: vs button setImage: forState:) because a background image will stretch with the frame while a normal image will not.

这篇关于iphone - 如何扩展按键触控区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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