일반 모서리에서 둥근 모서리로 CALayer를 전환하는 애니메이션을 만들려고합니다. UIBezierPath를 사용하기 때문에 위쪽 모서리 만 둥글게하고 싶습니다.CALayer 경로가있는 CABasicAnimation이 애니메이션을 적용하지 않습니다.
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"];
을하지만이 프로그램을 실행할 때, 모서리는 둥글게된다, 그러나 즉시 일에는 animation-가 없다 : 여기에 코드입니다. 그래서 비슷한 질문을 보았습니다. 그러나 그들은 내 문제를 해결하지 못했습니다.
Animating CALayer's shadowPath property
는 난 그냥 문제를 일으키는 하나 개의 작은 것은 잘못된 일을 해요 확신 해요,하지만 내 인생 내가 그것을 알아낼 수 없습니다.
솔루션 :
- (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;
}
}
정말 내가 할하는 데 필요한 모든 애니메이션 가치 '에서'적절한 설정했다.
솔루션 대신 질문의 일부의 답변으로 좋은 것입니다,하지만 어쨌든 도움이 +1 : – buildsucceeded