我有一个UIImageView显示一个圆圈,分为六个相等的三角形,对应于:
圆圈类似于以下内容(请原谅我糟糕的绘图):
我想从水龙头所在区域的触摸点得出。例如,如果用户点击圆圈的右上角,则该区域应为区域2:“> 60-120”。
我得到的输入数据是:
根据上述输入数据,有关如何推断抽头点落在哪个区域的任何建议?
答案 0 :(得分:8)
我刚刚注意到评论中提到的this solution中的一些错误,但它通常是您需要的...
我建议您在点击点和圈子的中心之间获取角度,然后实施getArea
功能,以便在您的圈子中点按特定区域,例如:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint tapPoint = [touch locationInView:self.view];
CGFloat angle = [self angleToPoint:tapPoint];
int area = [self sectionForAngle:angle];
}
- (float)angleToPoint:(CGPoint)tapPoint
{
int x = self.circle.center.x;
int y = self.circle.center.y;
float dx = tapPoint.x - x;
float dy = tapPoint.y - y;
CGFloat radians = atan2(dy,dx); // in radians
CGFloat degrees = radians * 180 / M_PI; // in degrees
if (degrees < 0) return fabsf(degrees);
else return 360 - degrees;
}
- (int)sectionForAngle:(float)angle
{
if (angle >= 0 && angle < 60) {
return 1;
} else if (angle >= 60 && angle < 120) {
return 2;
} else if (angle >= 120 && angle < 180) {
return 3;
} else if (angle >= 180 && angle < 240) {
return 4;
} else if (angle >= 240 && angle < 300) {
return 5;
} else {
return 6;
}
}
答案 1 :(得分:0)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let tapPoint = touch?.location(in: self)
let distance = distanceWithCenter(center: circleCenter,SCCenter: tapPoint!)
if distance <= radius {
let angle = angleToPoint(tapPoint: tapPoint!)
print(angle)
}
func angleToPoint(tapPoint:CGPoint) -> CGFloat{
let dx = circleCenter.x - tapPoint.x
let dy = circleCenter.y - tapPoint.y
let radians = atan2(dy, dx) + π // Angel in radian
let degree = radians * (180 / π) // Angel in degree
print("angle is off = \(degree)")
return degree
}
你现在检查它所属的角度。你可以通过比较来弄明白。如果你遇到任何问题,请告诉我。