gpt4 book ai didi

ios - 从屏幕中心展开一个圆圈

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:00:21 24 4
gpt4 key购买 nike

我正在尝试将圆从零半径扩展到屏幕中心的预定义半径。代码如下

在 viewDidLoad 中:-

- (void)viewDidLoad {

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[super viewDidLoad];

circleRadius = 0.0f;

circle = [CAShapeLayer layer];

[circle setAnchorPoint:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2)];
[circle setFillColor:[UIColor redColor].CGColor];
[circle setStrokeColor:[UIColor colorWithWhite:0.9f alpha:0.7f].CGColor];
[circle setLineWidth:0.0f];

[self.view.layer addSublayer:circle];


[self drawCircleWithRadius:circleRadius];


}

- (void)animateImageView {
circleRadius += 70.0f;
[self drawCircleWithRadius:circleRadius];
}

- (void)drawCircleWithRadius:(CGFloat)radius {

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
[pathAnimation setFromValue:(id)circle.path];//(id)circle.path
[pathAnimation setToValue:(id)[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0f * radius, 2.0f * radius) cornerRadius:radius].CGPath];
[pathAnimation setDuration:0.5f];
[pathAnimation setRepeatCount:1.0f];
[pathAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) cornerRadius:radius].CGPath;
circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius, CGRectGetMidY(self.view.frame)-radius);
[circle addAnimation:pathAnimation forKey:@"changePathAnimation"];
}

-animateImageView 在点击条形按钮时运行。不知何故,动画不那么流畅,圆圈似乎没有从屏幕中心增长。

请指出代码中的错误。谢谢。

最佳答案

试试下面的代码,这对我有用。我认为问题在于设置正确的路径。那么请问我是如何计算下面代码中的路径的。

- (void)viewDidLoad
{
[super viewDidLoad];

circleRadius = 200.0f;
CGPoint ptCenter = self.view.center;

circle = [CAShapeLayer layer];
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(ptCenter.x - circleRadius, ptCenter.y - circleRadius, 2.0f * circleRadius, 2.0f * circleRadius) cornerRadius:circleRadius].CGPath;

[circle setFillColor:[UIColor redColor].CGColor];
[circle setStrokeColor:[UIColor colorWithWhite:0.9f alpha:0.7f].CGColor];
[circle setLineWidth:0.0f];

[self.view.layer addSublayer:circle];
//[self drawCircleWithRadius:circleRadius];
}

- (IBAction)onTap:(id)sender
{
[self animateImageView];
}

- (void)animateImageView
{
circleRadius += 70.0f;
[self drawCircleWithRadius:circleRadius];
}

- (void)drawCircleWithRadius:(CGFloat)radius
{

CGPoint ptCenter = self.view.center;
CGRect rectBezierPath = CGRectMake(ptCenter.x - radius, ptCenter.y - radius, 2.0f * radius, 2.0f * radius);

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];

[pathAnimation setFromValue:(id)circle.path];
[pathAnimation setToValue:(id)[UIBezierPath bezierPathWithRoundedRect:rectBezierPath cornerRadius:circleRadius].CGPath];

[pathAnimation setDuration:0.5f];
[pathAnimation setRepeatCount:1.0f];

[pathAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];

circle.path = [UIBezierPath bezierPathWithRoundedRect:rectBezierPath cornerRadius:circleRadius].CGPath;
[circle addAnimation:pathAnimation forKey:@"changePathAnimation"];
}

关于ios - 从屏幕中心展开一个圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29893336/

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