Circle strokeWidth更改动画而不更改圆半径

问题描述 投票:0回答:1

我有几个矩形,分别用UIBezierPathCAShapeLayer画了一个圆。它们应该像按钮一样工作,在其中轻击它们时,它们被选择,而在轻按另一个按钮时,它们被取消选择,其中每种状态都应在这两种情况之间改变按钮的外观:

已取消选择:original已选择:enter image description here

这是通用代码(当选择更改时会调用:)

CGFloat radius = isSelected ? 9.8 : 13. ;
CGFloat strokeWidth = isSelected ? 10.5 : 2;

UIBezierPath *bezierPath = [UIBezierPath bezierPath];
CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
[bezierPath addArcWithCenter:center radius:radius startAngle:0 endAngle:2 * M_PI clockwise:YES];

CAShapeLayer *progressLayer = [[CAShapeLayer alloc] init];
[progressLayer setPath:bezierPath.CGPath];
[progressLayer setStrokeColor:[UIColor redColor].CGColor];
[progressLayer setLineWidth:strokeWidth]];
[self.layer addSublayer:progressLayer];

选择此矩形后,我只想向内更改圆的strokeWidth,并保持原始半径。取消选择它时,相反的情况会发生。如您所见,我同时更改了半径和lineWidth,因为当我增大strokeWidth时,外半径也随之增大。正如我所说,我希望保持不变。

问题是,我希望它以某种方式动画化,使宽度在选定时向内增大,在取消选定时增大,而根本不改变外半径。到目前为止,我得到的是strokeWidth和半径变化的动画,以使结果保持相同的半径。但这不是我所需要的。如您所见,此处仅对半径变化进行了动画处理,结果是一个不断增大的圆,我不希望这样做。

enter image description here

这是动画的代码(除了原始代码之外:]

  CABasicAnimation *changeCircle = [CABasicAnimation animationWithKeyPath:@"path"];
  changeCircle.duration = 0.6;
  changeCircle.fromValue = (__bridge id)oldPath; // current bezier path
  changeCircle.toValue = (__bridge id)newPath; // new bezier path

  [progressLayer addAnimation:changeCircle forKey:nil];

我也有相反的代码,该代码仅对strokeWidth的变化进行动画处理。它也不能满足我的需求。是否有一种方法可以在不更改外半径的情况下使strokeWidth增大或减小?

例如:enter image description hereenter image description hereenter image description here

ios objective-c uibezierpath cashapelayer cabasicanimation
1个回答
0
投票

感谢@Chris,我设法解决了。

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
pathAnimation.fromValue = (__bridge id) oldPath;
pathAnimation.toValue = (__bridge id) newPath;

CABasicAnimation *lineWidthAnimation = [CABasicAnimation animationWithKeyPath:@"lineWidth"];
lineWidthAnimation.fromValue = isSelected ? @2. : @10.5;
lineWidthAnimation.toValue = isSelected ? @10.5 : @2.;

CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = 0.3;
group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
group.animations = @[pathAnimation, lineWidthAnimation];

CAShapeLayer *progressLayer = [[CAShapeLayer alloc] init];
[progressLayer setPath:bezierPath.CGPath];
[progressLayer setStrokeColor:self.color.CGColor];
[progressLayer setLineWidth:isSelected ? 10.5 : 2];
[progressLayer setFillColor:[UIColor clearColor].CGColor];
[progressLayer addAnimation:group forKey:@"selectedAnimations"];
[self.layer addSublayer:progressLayer];

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.