Ich versuche, den Übergang eines CALayer von normalen Ecken zu abgerundeten Ecken zu animieren. Ich möchte nur die oberen Ecken abrunden, also benutze ich einen UIBezierPath. Hier ist der Code:CABasicAnimation mit CALayer-Pfad animiert nicht
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 dies ausführen, werden die Ecken abgerundet, aber es gibt keine Animations- es sofort passiert. Ich habe ähnliche Fragen hier auf SO gesehen, aber sie haben mein Problem nicht gelöst.
Animating CALayer's shadowPath property
Ich bin sicher, dass ich nur eine kleine Sache falsch mache, was das Problem verursacht, sondern für das Leben von mir, ich kann es nicht herausgefunden.
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 einen richtigen 'von' Wert für die Animation festgelegt wurde.
Lösung würde anstelle eines Teils der Frage als Antwort schön, aber das ist hilfreich sowieso so ein :) – buildsucceeded