CABasicAnimation ze ścieżką CALayer nie animuje

Próbuję animować przejście kalkulatora z normalnych narożników do zaokrąglonych narożników. Chcę tylko zaokrąglić górne rogi, więc używam ścieżki UIBezierPath. Oto kod:

UIRectCorner corners = UIRectCornerTopLeft | UIRectCornerTopRight;

UIBezierPath* newPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(8.0f, 8.0f)];
UIBezierPath* oldPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(0.0f, 0.0f)];

CAShapeLayer* layer = [CAShapeLayer layer];
layer.frame = self.bounds;
layer.path = oldPath.CGPath;

self.layer.mask = layer;

CABasicAnimation* a = [CABasicAnimation animationWithKeyPath:@"path"];
[a setDuration:0.4f];
[a setFromValue:(id)layer.path];
[a setToValue:(id)newPath.CGPath];

layer.path = newPath.CGPath;
[layer addAnimation:a forKey:@"path"];

Ale kiedy to uruchomię, rogi są zaokrąglone, ale nie ma animacji - dzieje się to natychmiast. Widziałem kilka podobnych pytań na temat SO, ale nie rozwiązali mojego problemu.

Animacja iPhone CALayer

Animowanie właściwości shadowPath CALayera

Jestem pewien, że robię tylko jedną drobną złą rzecz, która powoduje problem, ale dla mnie nie mogę tego zrozumieć.

Rozwiązanie:

- (void)roundCornersAnimated:(BOOL)animated {
    UIRectCorner corners = UIRectCornerTopLeft | UIRectCornerTopRight;

    UIBezierPath* newPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(8.0f, 8.0f)];
    UIBezierPath* oldPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(0.0001f, 0.0001f)];

    CAShapeLayer* layer = [CAShapeLayer layer];
    layer.frame = self.bounds;

    self.layer.mask = layer;

    if(animated) {
        CABasicAnimation* a = [CABasicAnimation animationWithKeyPath:@"path"];
        [a setDuration:0.4f];
        [a setFromValue:(id)oldPath.CGPath];
        [a setToValue:(id)newPath.CGPath];

        layer.path = newPath.CGPath;
        [layer addAnimation:a forKey:@"path"];
    } else {
        layer.path = newPath.CGPath;
    }
}

Naprawdę wszystko, co musiałem zrobić, to ustawić odpowiednią wartość „from” dla animacji.

questionAnswers(3)

yourAnswerToTheQuestion