gpt4 book ai didi

core-animation - 使用 CABasicAnimation 围绕其中心旋转 CAShapeLayer 弧

转载 作者:行者123 更新时间:2023-12-04 06:40:36 29 4
gpt4 key购买 nike

我有一个使用 CAShapeLayer 绘制的圆弧,我希望它围绕圆的中心旋转。我正在尝试使用“transform.rotation”属性上的 CABasicAnimation 来获取此动画。但是旋转似乎发生在与圆心不同的点上。

-(void)drawRect:(CGRect)rect{
int radius = 100;
CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) cornerRadius:radius].CGPath;
circle.fillColor = [UIColor orangeColor].CGColor;
circle.strokeColor = [UIColor blueColor].CGColor;
circle.lineWidth = 5;
circle.strokeStart = 0.0f;
circle.strokeEnd = 0.1f;
[self.layer addSublayer:circle];

CABasicAnimation *spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
spinAnimation.byValue = [NSNumber numberWithFloat:2.0f*M_PI];
spinAnimation.duration = 4;
spinAnimation.repeatCount = INFINITY;
[circle addAnimation:spinAnimation forKey:@"indeterminateAnimation"];
}

enter image description here

我知道这是一个老问题,但出路是什么。我通过设置图层的边界或 anchor 进行了很多修改,但它尚未围绕中心旋转。
circle.bounds = self.frame;

这是我将 View 添加到 View Controller 的方法。
[self.view addSubview:[[ProgressIndicator alloc] initWithFrame:CGRectMake(50, 50, 200, 200)]];

最佳答案

这就是我最终所做的。
错误是将动画添加到子层而不是层。
当只需要围绕圆心旋转时,无需在这里修改 anchorPoint 或 position 。

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];

int radius = 50;
CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) cornerRadius:radius].CGPath;
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor blueColor].CGColor;
circle.lineWidth = 5;
circle.strokeStart = 0.0f;
circle.strokeEnd = 0.1f;

[self.layer addSublayer:circle];
}

return self; }

- (void)drawRect:(CGRect)rect {

CABasicAnimation *spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
spinAnimation.byValue = [NSNumber numberWithFloat:2.0f*M_PI];
spinAnimation.duration = 4;
spinAnimation.repeatCount = INFINITY;

[self.layer addAnimation:spinAnimation forKey:@"indeterminateAnimation"]; }

关于core-animation - 使用 CABasicAnimation 围绕其中心旋转 CAShapeLayer 弧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27914358/

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