跳过按钮,只需对包含圆圈的视图中的触摸做出响应。
为您要捕获触摸的每个区域创建一个 CGPath,当您的 UIview 接收到触摸时,检查路径内的成员资格。
[编辑答案以显示骨架实现细节——TomH]
这是我解决问题的方法:(我还没有测试过这段代码,语法可能不太正确,但这是大体思路)
1) 使用 PS 或您喜欢的图像创建应用程序,创建四分之一圆的 png。将其添加到您的 XCode 项目中。
2) 将 UIView 添加到 UI。将 UIView 的图层内容设置为 png。
self.myView = [[UIView alloc] initWithRect:CGRectMake(10.0, 10.0, 100.0, 100,0)];
[myView.layer setContents:(id)[UIImage loadImageNamed:@"my.png"]];
3) 创建描述 UIView 中您感兴趣的区域的 CGPath。
self.quadrantOnePath = CGPathCreateMutable();
CGPathMoveToPoint(self.quadrantOnePath, NULL, 50.0, 50.0);
CGPathAddLineToPoint(self.quadrantOnePath, NULL, 100.0, 50.0);
CGPathAddArc(self.quadrantOnePath, NULL, 50.0, 50.0, 50.0, 0.0, M_PI2, 1);
CGPathCloseSubpath(self.quadrantOnePath);
// create paths for the other 3 circle quadrants too!
4) 添加 UIGestureRecognizer 并监听/观察视图中的点击
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[tapRecognizer setNumberOfTapsRequired:2]; // default is 1
5) 当 tapRecognizer 调用它的目标选择器时
- (void)handleGesture:(UIGestureRecognizer *) recognizer {
CGPoint touchPoint = [recognizer locationOfTouch:0 inView:self.myView];
bool processTouch = CGPathContainsPoint(self.quadrantOnePath, NULL, touchPoint, true);
if(processTouch) {
// call your method to process the touch
}
}
不要忘记在适当的时候释放所有东西——使用 CGPathRelease 释放路径。
另一个想法:如果您用来表示圆形象限的图形只是一种填充颜色(即没有花哨的图形、图层效果等),您还可以使用您在 UIView 的 drawRect 方法中创建的路径来也画出象限。这将解决上述方法的缺点之一:图形和用于检查触摸的路径之间没有紧密集成。也就是说,如果您将图形换成不同的东西,更改图形的大小等,用于检查触摸的路径将不同步。可能是一段需要大量维护的代码。