重叠的圆形按钮和点击/点击测试 [英] Overlapping Circular Buttons and Hit/Tap Testing

查看:89
本文介绍了重叠的圆形按钮和点击/点击测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Tabla鼓应用程序(一个Table是一对连接在一起的小手鼓)。为此,我想通过在鼓的每个部分上使用一个按钮来将其放置在另一个之上。像这样:

I am trying to create a Tabla drum app (a Table is a pair of small hand drums attached together). To do this I want to use a button for each section of the drum by putting one on top of another. Like so:

我尝试为按钮使用圆形图像,但周围区域为矩形。这意味着当用户点击绿色圆圈的一部分时,它将播放橙色圆圈的声音(我希望这是有道理的。)。

I've tried using a circular image for the button but the surrounding area are a rectangle. That means when the user would hit parts of the green circle it plays the sound for the orange circle (I hope that makes sense).

如何确保

推荐答案

没有简单方法来限制抽头检测区域。您是完全正确的:按钮的点击检测区域是矩形,这就是它所知道的全部。因此,您无法使用按钮的简单点击检测来完成您的工作。您将不得不自己检测一下拍子,看看它在哪里,并利用该知识以及您对圆圈图的了解,来确定用户敲击鼓的哪一部分。

There is no "simple way" to limit the tap-detection area. You are perfectly correct: the button's tap-detection area is its rectangle, and that's all it knows about. Therefore, you can't use the button's simple tap-detection to do the work for you. You are going to have to detect the tap yourself, look at where it is, and use that knowledge plus your knowledge of where the drawings of the circles are to decide which section of the drum the user tapped in.

查看UITapGestureRecognizer,以了解用户点击的位置以及在何处。

Look at UITapGestureRecognizer for a way to find out that the user tapped and where.

查看UIBezierPath,以进行点击检测相对于形状(例如,圆形,回答问题,此圆上的这个点)。

Look at UIBezierPath for a way to do hit detection with respect to a shape (e.g. a circle, answering the question, "is this point in this circle").

您最好的选择可能是修改内置的命中测试,即UIButton或UIView子类,其中您重写 pointInside:withEvent:仅在该点位于内部圆内时返回YES,例如:

Your best bet might be to modify the built-in hit-testing, i.e. a UIButton or UIView subclass in which you override pointInside:withEvent: to return YES only if the point is within the interior circle, e.g.:

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
    return [p containsPoint:point];
}

这就像它将变得简单一样,害怕。

That is about as "simple" as it's going to get, I'm afraid.

这篇关于重叠的圆形按钮和点击/点击测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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