Jon*_*han 16 objective-c cabasicanimation cashapelayer ios
我正在绘制一个圆,初始半径为200
self.circle = [CAShapeLayer layer];
self.circle.fillColor = nil;
self.circle.strokeColor = [UIColor blackColor].CGColor;
self.circle.lineWidth = 7;
self.circle.bounds = CGRectMake(0, 0, 2 * radius, 2 * radius);
self.circle.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.radius, self.radius)
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我如何将变化动画为半径100?
Jon*_*han 23
This is how I ended up doing it:
UIBezierPath *newPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(newRadius, newRadius) radius:newRadius startAngle:(-M_PI/2) endAngle:(3*M_PI/2) clockwise:YES];
CGRect newBounds = CGRectMake(0, 0, 2 * newRadius, 2 * newRadius);
CABasicAnimation* pathAnim = [CABasicAnimation animationWithKeyPath: @"path"];
pathAnim.toValue = (id)newPath.CGPath;
CABasicAnimation* boundsAnim = [CABasicAnimation animationWithKeyPath: @"bounds"];
boundsAnim.toValue = [NSValue valueWithCGRect:newBounds];
CAAnimationGroup *anims = [CAAnimationGroup animation];
anims.animations = [NSArray arrayWithObjects:pathAnim, boundsAnim, nil];
anims.removedOnCompletion = NO;
anims.duration = 2.0f;
anims.fillMode = kCAFillModeForwards;
[self.circle addAnimation:anims forKey:nil];
Run Code Online (Sandbox Code Playgroud)
Thanks to Jacob for pointing me in the right direction.
Cha*_*ood 12
我相信这是更简单和首选的方法:
UIBezierPath *newPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(newRadius, newRadius) radius:newRadius startAngle:(-M_PI/2) endAngle:(3*M_PI/2) clockwise:YES];
CABasicAnimation* pathAnim = [CABasicAnimation animationWithKeyPath:@"path"];
pathAnim.fromValue = (id)self.circle.path;
pathAnim.toValue = (id)newPath.CGPath;
pathAnim.duration = 2.0f;
[self.circle addAnimation:pathAnim forKey:@"animateRadius"];
self.circle.path = newPath.CGPath;
Run Code Online (Sandbox Code Playgroud)
我从Apple文档中获得了这种方法(参见清单3-2).这里最重要的事情是,你设置fromValue
和最终值也设置self.circle
的path
,以newPath.CGPath
正确设置了动画后.动画不会改变模型的基础值,因此在另一种方法中,您实际上并未对形状图层的path
变量进行永久性更改.
我使用(使用像过去所选择的答案的解决方案removedOnCompletion
和fillMode
等),但我发现,iOS上的某些版本中他们造成内存泄露,也有时这种做法只是没有出于某种原因.
使用这种方法,您可以显式创建动画(从它开始和结束的位置),并且您在末尾指定路径的最终永久值(使用最后一行),因此不需要在完成动画时不要删除动画(我相信如果你多次执行这个动画会导致内存泄漏......未经删除的动画会在内存中积累).
另外,我删除了动画,bounds
因为你不需要它来为圆圈设置动画.即使路径被绘制到图层的边界之外,它仍然会被完全显示出来(请参阅相关文档CAShapeLayer
).如果由于某些其他原因确实需要为边界设置动画,则可以使用相同的方法(确保指定fromValue
边界的最终值).
归档时间: |
|
查看次数: |
12083 次 |
最近记录: |