简体   繁体   English

弧在圆上的位置

[英]Arc's position on circle

I've got few pixels wide circle (circle is UIBezierPath based). 我有几个像素的宽圆圈(圆圈是基于UIBezierPath的)。 I have to put an arc (which is basically UIView subclass with custom drawing) on the circle so the arc covers circle. 我必须在圆上放置一个弧(基本上是带有自定义绘图的UIView子类),以便圆弧覆盖圆圈。 I know how to calculate rotation of arc and position but something is not right. 我知道如何计算弧和位置的旋转,但有些事情是不对的。 I mean I know the reason - it's beacause center property which is assigned to center of UIView, if it was center of the arc, everything would be great. 我的意思是我知道原因 - 它是因为中心属性被分配到UIView的中心,如果它是弧的中心,一切都会很棒。 But it's not. 但事实并非如此。

I also know how to solve that. 我也知道如何解决这个问题。 I have to calculate smaller radius where I will put arcs on. 我必须计算较小的半径,我将把弧线放在上面。 But how to do that. 但是如何做到这一点。 Basically it seems easy but because of the arc is in rectangular UIView it gets a bit harder. 基本上它似乎很容易,但由于弧线是矩形UIView,它变得有点困难。 I'll show you some images so you can see the problem. 我会给你看一些图片,这样你就可以看到问题所在。

在此输入图像描述在此输入图像描述

The easiest way to do this is to change the anchor point of each arc view's layer. 最简单的方法是更改​​每个弧视图图层的锚点。 You can read about the anchor point here if you don't already know about it. 如果你还不知道它, 你可以在这里阅读锚点 You will need to add the QuartzCore framework to your build target and add #import <QuartzCore/QuartzCore.h> . 您需要将QuartzCore框架添加到构建目标并添加#import <QuartzCore/QuartzCore.h>

CGRect circleBounds = circleView.bounds;

topArcView.layer.anchorPoint = CGPointMake(.5, 0);
topArcView.layer.position = CGPointMake(CGRectGetMidX(circleBounds), 0);

bottomArcView.layer.anchorPoint = CGPointMake(.5, 1);
bottomArcView.layer.position = CGPointMake(CGRectGetMidX(circleBounds), CGRectGetMaxY(circleBounds));

leftArcView.layer.anchorPoint = CGPointMake(0, .5);
leftArcView.layer.position = CGPointMake(circleBounds.origin.x, CGRectGetMidY(circleBounds));

rightArcView.layer.anchorPoint = CGPointMake(1, .5);
rightArcView.layer.position = CGPointMake(CGRectGetMaxX(circleBounds), CGRectGetMidY(circleBounds));

Perhaps you could make the UIView subclass the same size and center point as the circle's rect. 也许你可以使UIView子类与圆的矩形具有相同的大小和中心点。 Then draw the arc in the correct location. 然后在正确的位置绘制弧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM