Próbuję animować przejście CALayer od normalnych narożników do zaokrąglonych narożników. Chcę tylko zaokrąglić górne rogi, więc używam UIBezierPath. Oto kod:CABasicAnimation ze ścieżką CALayer nie animuje
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 gdy uruchamiam to, narożniki są zaokrąglone, ale nie ma animation- zdarza się natychmiast. Widziałem kilka podobnych pytań tutaj na SO, ale nie rozwiązały mojego problemu.
Animating CALayer's shadowPath property
jestem pewien, że jestem po prostu robi jeden drobiazg źle który jest przyczyną problemu, ale dla życia mnie nie mogę 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 potrzebne do zrobienia została ustawiona właściwa 'od' wartości dla animacji.
rozwiązaniem byłoby miłe, jako odpowiedź zamiast części pytania, ale jest to pomocne tak więc +1 :) – buildsucceeded