Tôi muốn tạo thanh tiến trình có dạng vòng cung. Màu của thanh tiến trình phải thay đổi theo các giá trị.Áp dụng màu gradient cho cung được tạo bằng UIBezierPath
Tôi đã tạo một vòng cung bằng cách sử dụng UIBezierPath bezierPathWithArcCenter
. Tôi sử dụng đoạn mã sau:
- (void)viewDidLoad
{
[super viewDidLoad];
int radius = 100;
CAShapeLayer *arc = [CAShapeLayer layer];
arc.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 50) radius:radius startAngle:60.0 endAngle:0.0 clockwise:YES].CGPath;
arc.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
CGRectGetMidY(self.view.frame)-radius);
arc.fillColor = [UIColor clearColor].CGColor;
arc.strokeColor = [UIColor purpleColor].CGColor;
arc.lineWidth = 15;
[self.view.layer addSublayer:arc];
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 5.0; // "animate over 10 seconds or so.."
drawAnimation.repeatCount = 1.0; // Animate only once..
drawAnimation.removedOnCompletion = NO; // Remain stroked after the animation..
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:10.0f];
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[arc addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
}
Kết quả trông như thế này:
Câu hỏi của tôi là: Làm thế nào để áp dụng một gradient với màu nếu ví dụ: giá trị là < = 50%? Tôi cũng tạo ra một UIButton tạo ra các số CGFloat ngẫu nhiên để nối nó với thanh tiến trình. Có ai có một ý tưởng làm thế nào để giải quyết điều này?
Gradient sẽ giống như thế này:
Bất kỳ trợ giúp sẽ được đánh giá cao!
Cảm ơn bạn rất nhiều.
Granit
Cảm ơn bạn rất nhiều! Nó làm việc như một nét duyên dáng :) – Granit
Vui mừng nó đã giúp :) – djshiow
Điều này không giống như nó sẽ vẽ ** dọc theo ** đường dẫn, nhưng thay vào đó, nó có vẻ như nó sẽ vẽ một gradient ngang, được tiết lộ bởi đường dẫn . Điều đó có ý nghĩa? Làm thế nào để bạn "uốn cong" một gradient ngang để theo một vòng tròn đầy đủ? – tracicot