ios - 如何在iOS中将度数转换为极坐标

标签 ios objective-c

我在 UILabel 中画了一个圆。我为圆圈设置了开始度和结束度。现在我想将 imageview 添加到这个圆标签中,Imageview 的极坐标与圆的结束度相匹配。因此,我想将圆的端度转换为极坐标。示例:

Width of UILabel = 62
Height of UILabe = 62
Start degree of cirle in UILabel  = 0 (degree)
End degree of cirle in UILabel  = 150 (degree)

我将 UIImageView(pointImgView) 添加到这个 UILabel,如下所示

  UIImageView *pointImgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, 6, 6)];
 [self addSubview:pointImgView];

我不知道如何将圆的结束度数(150 度)转换为 pointImgView 的极坐标。

请给我一些建议。非常感谢

最佳答案

使用这些宏。

#define kDCControlDegreesToRadians(x) (M_PI * (x) / 180.0)
#define kDCControlRadiansToDegrees(x) ((x) * 180.0 / M_PI)

代码标准注释:如果您在整个应用程序中使用此计算,请使用此宏。如果你在一个地方使用它,最好使用这个逻辑在你的 View Controller 中编写一个方法。

更新:

我的建议:不要使用 ImageView ,而是用图像填充标签颜色 my backgroundColor = [UIColor colorWithPatternImage:] 并通过以下代码在标签周围画圈。 使用以下代码以起始角度绘制圆。

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGRect boundsRect = self.bounds;
    float x = boundsRect.size.width / 2;
    float y = boundsRect.size.height / 2;
    boundsRect.size.width -= 5;
    boundsRect.size.height -= 5;

    CGContextSaveGState(context);
    CGContextSetLineWidth(context, self.valueArcWidth);
    [self.trackingColor set];
    // Drawing code
    CGFloat valueAdjusted = (self.value - self.minimumValue) / (self.maximumValue - self.minimumValue);
    CGContextAddArc(context,
                    x,
                    y,
                    boundsRect.size.width / 2 -2,
                    kDCControlDegreesToRadians(self.arcStartAngle ,
                    kDCControlDegreesToRadians(self.arcStartAngle + (360 * valueAdjusted),
                    0);
    CGContextStrokePath(context);
    CGContextRestoreGState(context);
}

此处MinimumValue = 10.0maximum value = 11.0,您的值必须介于(10 -11)之间。那就是将您的值转换为 0.0 到 1.0 并将其添加到 10.0。 注意:我给出我的代码,根据您的情况进行编辑。

关于ios - 如何在iOS中将度数转换为极坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20715707/

相关文章:

ios - 后台获取替代方案

ios - 没有已知的类- iOS

ios - "sudo pod install"错误

IOS MDM 拒绝 checkin 请求不起作用

ios - 方向 : portrait & landscape in Xcode 4. 2

ios - 在 iOS 上播放多种声音会产生变形金刚般的噪音

ios - 如何限制对依赖Internet连接的ViewController的访问?

ios - 使用 iOS Keychain Services 保存用户名-密码。如何以及安全程度如何?

objective-c - 将 Objective-C typedef 转换为 Swift 枚举后避免 "Interface type cannot be statically allocated"

objective-c - 在 Objective-C 中实现真/假/未定义三分变量的最佳方法