【问题标题】:How to Animate a UIBezierPath如何为 UIBezierPath 设置动画
【发布时间】:2012-06-07 19:55:04
【问题描述】:

我想将矩形的 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];
}

【问题讨论】:

    标签: objective-c animation calayer cgpath uibezierpath


    【解决方案1】:

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

    CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"];
    morph.duration = 5;
    morph.toValue = (id) [self getTrianglePath];
    [maskLayer addAnimation:morph forKey:nil];
    

    第一行声明您要定义一个动画来更改图层的path 属性。第二行表示动画需要五秒钟,第三行给出动画的最终状态。最后,我们将动画添加到图层。键是动画的唯一标识符,也就是说,如果添加两个具有相同键的动画,则只考虑最后一个。该键还可用于覆盖所谓的隐式动画。 CALayer 有几个默认动画的属性。更准确地说,如果您更改这些属性之一,则更改的动画持续时间为 0.25。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多