Tôi đang cố gắng tạo hoạt ảnh cho việc chuyển đổi một CALayer từ các góc thông thường thành các góc tròn. Tôi chỉ muốn làm tròn các góc trên cùng, vì vậy tôi đang sử dụng UIBezierPath. Đây là mã:CABasicAnimation với đường dẫn CALayer không hoạt ảnh
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"];
Nhưng khi tôi chạy điều này, các góc được làm tròn, nhưng không có hoạt ảnh - nó xảy ra ngay lập tức. Tôi đã thấy một số câu hỏi tương tự ở đây trên SO, nhưng họ đã không giải quyết vấn đề của tôi.
Animating CALayer's shadowPath property
Tôi chắc chắn rằng tôi chỉ làm một điều ít sai mà gây ra vấn đề, nhưng đối với cuộc sống của tôi, tôi không thể hình dung nó ra.
Giải pháp:
- (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;
}
}
Thật tất cả những gì cần thiết để làm được thành một cái tên từ 'giá trị cho các hình ảnh động.
Giải pháp sẽ được tốt đẹp như một câu trả lời thay vì là một phần của câu hỏi, nhưng điều này là hữu ích anyway vì vậy 1 :) – buildsucceeded