iOS:CAShapeLayer路径转换

时间:2013-07-25 13:00:47

标签: ios core-graphics cashapelayer

我在对路径进行基本转换之前使用过CAShape Layer - 从较小的圆圈到较大的圆圈。很好,但后来我尝试将一个三角形变成一个圆圈;它有效,但转型很奇怪。换句话说,从一种形状到另一种形状,它在最终形状形成之前“翻转”,“扭曲”。使用相同的形状,没有问题 - 从圆形到圆形 - 但是形状不同,转换很奇怪。

我想知道这是否是预期的方式?或者是否有其他技巧或变通方法,我们可以使用CAShape Layer顺利地按比例将一个形状转换为另一种形状(或者有其他方法可以做到这一点;它不一定是CAShape Layer)?提前致谢。

3 个答案:

答案 0 :(得分:10)

通过仔细选择您的控制点等,您可以制作一个绘制为三角形的路径,但其具有与您想要绘制的圆相同数量的分段和控制点。像这样(所有数值都假设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提供的天真/免费解决方案不太可能符合您的要求。

答案 1 :(得分:1)

来自path上的CAShapeLayer媒体资源的文档:

  

如果两条路径具有不同数量的控制点或分段,则结果未定义。

所以是的,我会说这是预期的,因为圆形和三角形是不同的形状。

This article详细描述了自定义路径动画。

答案 2 :(得分:1)

我猜这里!您可以使用3个点定义圆圈。这样,您将获得与三角形相同的点数。要定义圈子,这将是您最好的朋友

+ (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;