gpt4 book ai didi

ios - 沿着 UIBezierPath 放置 `points`,使得 `center` 点位于路径上

转载 作者:行者123 更新时间:2023-11-28 19:32:21 28 4
gpt4 key购买 nike

问题陈述:

创建一个圆周上有圆点的半圆形刻度盘。用户可以平移和旋转这个表盘。

enter image description here

方法:

  • 创建了一个UIView
  • 添加了一个半径为 UIView 高度两倍的圆(使用 [UIBezierPath bezierPathWithOvalInRect]CAShapeLayer)
  • 沿着这个圆的圆周添加(圆形CALayer对象),使得这些点的中心位于圆周上。

障碍:

点是沿着路径绘制的,但这些点的中心不在圆周上。

enter image description here

代码如下:

@property (nonatomic, retain) CAShapeLayer* dialLineLayer;

- (instancetype)initWithCoder:(NSCoder *)aDecoder {

if (self = [super initWithCoder:aDecoder]) {

self.dialLineLayer = [CAShapeLayer layer];
CGRect path = CGRectMake(SCREEN_WIDTH /2 - self.radius, -self.radius, 2 * self.radius, 2 * self.radius);

[self.dialLineLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:path] CGPath]];
[self.dialLineLayer setStrokeColor:[[UIColor la_white10Color] CGColor]];
[self.dialLineLayer setFillColor:[[UIColor clearColor] CGColor]];
[self.dialLineLayer setLineWidth:3.0f];
[self.layer addSublayer:self.dialLineLayer];

[self addDotsOnLineLayer];
}

return self;
}

- (CGFloat) radius {

// View has an aspect ratio constraint of 75:34 (W:H)

return SCREEN_WIDTH * 34 / 75 - 10; // Circle radius is 10px less that View's height
}

- (CGFloat) circumference {

return M_2_PI * self.radius; // 2Pi * radius
}

- (void) addDotsOnLineLayer {

CGPoint dotPoint = CGPointMake(SCREEN_WIDTH /2 - self.radius - self.radius/2, 0);

for (double t = 0; t < 2*M_PI; t += 0.2) {

CGFloat dotRadius = arc4random_uniform(10) - arc4random_uniform(3); // Random radius between 3px and 10px

CALayer* dotLayer = [CALayer layer];
dotLayer.frame = CGRectMake(dotPoint.x, dotPoint.y, dotRadius, dotRadius);
dotLayer.cornerRadius = dotRadius / 2;
dotLayer.masksToBounds = YES;
dotLayer.backgroundColor = [UIColor greenColor].CGColor;

dotPoint.x = self.radius * cos(t) + (SCREEN_WIDTH / 2);
dotPoint.y = self.radius * sin(t) + 0;

[self.dialLineLayer addSublayer:dotLayer];
}
}

最佳答案

试试这个:(支撑主要 'doh' + facepalm ;))

dotLayer.frame = CGRectMake(0, 0, dotRadius, dotRadius);
dotLayer.position = dotPoint;

解释

您目前正在将图层的原点 定位在路径上。 (去掉拐角半径,你就会明白我的意思了。)但是,由于你想要路径上图层的中心,你必须分配给.position .

请注意,只要您将图层的 .anchorPoint 属性保留在 {0.5, 0.5},这就是正确的。参见 Core Animation Basics了解更多信息。

关于ios - 沿着 UIBezierPath 放置 `points`,使得 `center` 点位于路径上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42575445/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com