我想将矩形的 UIBezierPath 动画化为三角形,而不是为路径上的 View 设置动画,而是为路径本身设置动画,使其从一种形状变为另一种形状。路径被添加到 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 的持续时间进行动画处理。
关于objective-c - 如何为 UIBezierPath 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10869509/