CABasicAnimation mit CALayer-Pfad wird nicht animiert

Ich versuche, die animation der übergang von einem CALayer von normalen Ecken zum abgerundeten Ecken. Ich möchte nur das Runde an den oberen Ecken, so bin ich mit einem UIBezierPath. Hier ist der code:

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"];

Aber wenn ich diesen starte, die Ecken sind abgerundet, aber es gibt keine animation - es geschieht sofort. Ich habe gesehen einige ähnliche Fragen hier auf, SO, aber Sie hat nicht mein problem zu lösen.

iPhone CALayer-animation

Animieren CALayer ist shadowPath Eigenschaft

Ich bin mir sicher, ich mache nur eine kleine Sache falsch, die das problem verursacht, aber für das Leben von mir ich kann T es herausfinden.

Lösung:

- (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;
    }
}

Wirklich alles, was ich tun musste, um eine korrekte 'aus' - Wert für die animation.

InformationsquelleAutor der Frage eric.mitchell | 2012-07-13

Schreibe einen Kommentar