iOS: 给定一个圆得出抽头点的角度

iOS: derive angle of tap point given a circle

我有一个 UIImageView 显示一个分成六个相等三角形的圆,对应于:

圆形类似于下图(画的不好请见谅):

我想从触摸点推导出水龙头在哪个区域。例如,如果用户点击圆圈的右上角,则该区域应为区域 2:“>60-120”。

我得到的输入数据是:

关于如何根据上述输入数据推断分接点落在哪个区域的任何建议?

我刚刚注意到我在评论中提到的 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;
    }
}
    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
        }

你得到了角度现在检查它在哪个部分 belong.You 可以解决 comparision.If 你遇到任何问题请告诉我。