Objective c 如何设置UIBezierPath的动画

Objective c 如何设置UIBezierPath的动画,objective-c,animation,calayer,cgpath,uibezierpath,Objective C,Animation,Calayer,Cgpath,Uibezierpath,我希望将矩形的UIBezierPath设置为三角形,而不是对路径上的视图设置动画,而是对路径本身设置动画,使其从一个形状变形为另一个形状。路径将添加到CALayer CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.fillColor = [[UIColor whiteColor] CGColor]; maskLayer.path = [self getRectPath]; -(CGPathRef)getTriangle

我希望将矩形的UIBezierPath设置为三角形,而不是对路径上的视图设置动画,而是对路径本身设置动画,使其从一个形状变形为另一个形状。路径将添加到CALayer

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.fillColor = [[UIColor whiteColor] CGColor];    
maskLayer.path = [self getRectPath];

-(CGPathRef)getTrianglePath
{
    UIBezierPath* triangle = [UIBezierPath bezierPath];
    [triangle moveToPoint:CGPointZero];
    [triangle addLineToPoint:CGPointMake(width(self.view),0)];
    [triangle addLineToPoint:CGPointMake(0, height(self.view))];
    [triangle closePath];
    return [triangle CGPath];
}

-(CGPathRef)getRectPath
{
    UIBezierPath*rect = [UIBezierPath bezierPathWithRect:self.view.frame];
    return [rect CGPath];
}

我不是CoreAnimation方面的专家,但您可以定义
CABasicAnimation
,如下所示

CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"];
morph.duration = 5;
morph.toValue = (id) [self getTrianglePath];
[maskLayer addAnimation:morph forKey:nil];
第一行表示要定义一个动画来更改层的
path
特性。第二行表示动画需要五秒钟,第三行表示动画的最终状态。最后,我们将动画添加到层中。关键点是动画的唯一标识符,也就是说,如果使用同一关键点添加两个动画,则只考虑最后一个。此键还可用于覆盖所谓的隐式动画。默认情况下设置动画的
CALayer
有几个属性。更准确地说,如果更改其中一个属性,更改将以0.25的持续时间设置动画