通过仔细选择控制点等,您可能会设计出一条绘制为三角形的路径,但它与您要绘制的圆具有相同数量的线段和控制点。像这样(所有数值都假设 iPhone 屏幕,但要点是一般性的):
@implementation ViewController
{
CAShapeLayer* shapeLayer;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
shapeLayer = [[CAShapeLayer alloc] init];
CGRect bounds = self.view.bounds;
bounds.origin.x += 0.25 * bounds.size.width;
bounds.size.width *= 0.5;
bounds.origin.y += 0.25 * bounds.size.height;
bounds.size.height *= 0.5;
shapeLayer.frame = bounds;
shapeLayer.backgroundColor = [[UIColor redColor] CGColor];
[self.view.layer addSublayer: shapeLayer];
[self toCircle: nil];
}
CGPoint AveragePoints(CGPoint a, CGPoint b)
{
return CGPointMake((a.x + b.x) * 0.5f, (a.y + b.y) * 0.5f);
}
- (IBAction)toCircle:(id)sender
{
UIBezierPath* p = [[UIBezierPath alloc] init];
[p moveToPoint: CGPointMake(80, 56)];
[p addCurveToPoint:CGPointMake(144, 120) controlPoint1:CGPointMake(115.34622, 56) controlPoint2:CGPointMake(144, 84.653778)];
[p addCurveToPoint:CGPointMake(135.42563, 152) controlPoint1:CGPointMake(144, 131.23434) controlPoint2:CGPointMake(141.0428, 142.27077)];
[p addCurveToPoint:CGPointMake(48, 175.42563) controlPoint1:CGPointMake(117.75252, 182.61073) controlPoint2:CGPointMake(78.610725, 193.09874)];
[p addCurveToPoint:CGPointMake(24.574375, 152) controlPoint1:CGPointMake(38.270771, 169.80846) controlPoint2:CGPointMake(30.191547, 161.72923)];
[p addCurveToPoint:CGPointMake(47.999996, 64.574379) controlPoint1:CGPointMake(6.9012618, 121.38927) controlPoint2:CGPointMake(17.389269, 82.24749)];
[p addCurveToPoint:CGPointMake(80, 56) controlPoint1:CGPointMake(57.729225, 58.957207) controlPoint2:CGPointMake(68.765656, 56)];
[p closePath];
[CATransaction begin];
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
pathAnimation.duration = 3.f;
pathAnimation.fromValue = (id)shapeLayer.path;
pathAnimation.toValue = (id)p.CGPath;
[shapeLayer addAnimation:pathAnimation forKey:@"path"];
[CATransaction setCompletionBlock:^{
shapeLayer.path = p.CGPath;
}];
[CATransaction commit];
double delayInSeconds = 4.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self toTriangle: nil];
});
}
- (IBAction)toTriangle: (id)sender
{
UIBezierPath* p = [[UIBezierPath alloc] init];
// Triangle using the same number and kind of points...
[p moveToPoint: CGPointMake(80, 56)];
[p addCurveToPoint: AveragePoints(CGPointMake(80, 56), CGPointMake(135.42563, 152)) controlPoint1:CGPointMake(80, 56) controlPoint2:AveragePoints(CGPointMake(80, 56), CGPointMake(135.42563, 152))];
[p addCurveToPoint:CGPointMake(135.42563, 152) controlPoint1:AveragePoints(CGPointMake(80, 56), CGPointMake(135.42563, 152)) controlPoint2:CGPointMake(135.42563, 152)];
[p addCurveToPoint:AveragePoints(CGPointMake(135.42563, 152), CGPointMake(24.574375, 152)) controlPoint1:CGPointMake(135.42563, 152) controlPoint2:AveragePoints(CGPointMake(135.42563, 152), CGPointMake(24.574375, 152))];
[p addCurveToPoint:CGPointMake(24.574375, 152) controlPoint1:AveragePoints(CGPointMake(135.42563, 152), CGPointMake(24.574375, 152)) controlPoint2:CGPointMake(24.574375, 152)];
[p addCurveToPoint: AveragePoints(CGPointMake(24.574375, 152),CGPointMake(80, 56)) controlPoint1:CGPointMake(24.574375, 152) controlPoint2:AveragePoints(CGPointMake(24.574375, 152),CGPointMake(80, 56)) ];
[p addCurveToPoint:CGPointMake(80, 56) controlPoint1:AveragePoints(CGPointMake(24.574375, 152),CGPointMake(80, 56)) controlPoint2:CGPointMake(80, 56)];
[p closePath];
[CATransaction begin];
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
pathAnimation.duration = 3.f;
pathAnimation.fromValue = (id)shapeLayer.path;
pathAnimation.toValue = (id)p.CGPath;
[shapeLayer addAnimation:pathAnimation forKey:@"path"];
[CATransaction setCompletionBlock:^{
shapeLayer.path = p.CGPath;
}];
[CATransaction commit];
double delayInSeconds = 4.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self toCircle: nil];
});
}
即使段数和控制点的数量相同,动画仍然看起来有点不稳定,可能不是您想要的。原因是 CA 似乎一对一地匹配所有段和控制点,然后在它们之间为所有点进行线性插值。因为控制点和生成的路径之间的关系不是线性的(在这种情况下是三次方),所以线性插值控制点位置不会导致路径以线性方式移动。如果你运行这段代码,你可以看到它在过渡时,在三角形的一侧有奇怪的隆起,圆形路径在圆弧中还有其他点。
更一般地说,期望 CA 以某种特定方式在两条任意路径之间神奇地变形,这在外观上是可取的,这是不合理的。即使努力手动构建这些路径,以便它们可以祈祷变形,它们仍然没有看起来像我认为的那样。
通过使用扁平路径(即由许多小直线而不是弯曲路径元素组成的路径)来实现所需的效果可能更合理。即使这样看起来也不是微不足道的,因为您将再次需要两条路径具有相同数量的段,并且您必须构建这些段,以使公共点是沿整体的正确数量的段小路。
总而言之:这是一个相当复杂的问题,CoreAnimation 提供的幼稚/免费的解决方案不太可能是您想要的。