2

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

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

4

3 回答 3

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 提供的幼稚/免费的解决方案不太可能是您想要的。

于 2013-07-25T17:08:41.117 回答
1

path财产的文档中CAShapeLayer

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

所以是的,我会说这是意料之中的,因为圆形和三角形是不同的形状。

本文非常详细地描述了自定义路径动画。

于 2013-07-25T13:03:47.413 回答
1

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

+ (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
于 2013-07-25T13:07:54.067 回答