UIView with custom drawRect and animateWithDuration

I have a subclass UIViewthat shows a circular move:

circular progress view

The screenshot shows the progress with the value 0.667.

Inside my subclasses UIViewin the method, drawRect:I draw two circles: one for the background oval (gray), the second for the actual oval move (blue). (Check source code)

It works as expected, but does not allow you to animate progress changes. I tried to find a solution that allows using the class method UIView animateWithDuration:animations:as follows:

MyCustomView *progressView = ...
[UIView animateWithDuration:3 
                 animations:^{
                     progressView.progressValue = 1.f;
                 }];

But I could not make it come to life.

You can check the source code of my GitHub repository .

, , progressValue . , ( UIView animateWithDuration:animations:), , . CALayer UIView. , , custom_layer. CALayer .

: UIView, (progressValue), UIView animateWithDuration:animations: (progressValue)?

UPDATE

rounak answer drawRect: CAShapeLayer s. , progressValue, strokeEnd , , , ( ).

actionForLayer:forKey: ( ), CABasicAnimation, strokeEnd, progressValue .

, :

__block NSDate *date = [NSDate date];
[UIView animateWithDuration:3
                 animations:^{
                     self.progressView.progressValue = 1.f;
                 }
                 completion:^(BOOL finished1) {
                     NSLog(@"first animation completed in %f", [[NSDate date] timeIntervalSinceDate:date]);
                     date = [NSDate date];
                     [UIView animateWithDuration:3
                                      animations:^{
                                          self.progressView.progressValue = 0.f;
                                      }
                                      completion:^(BOOL finished2) {
                                          NSLog(@"second animation completed in %f", [[NSDate date] timeIntervalSinceDate:date]);
                                          [self animateProgress];
                                      }];
                 }];

progressValue, , 3 . , ( ). :

enter image description here

, , , [CABasicAnimation animationWithKeyPath:@"strokeEnd"] strokeEnd ( ).

, , .

2

, setProgressValue: setter:

self.progressOvalLayer.strokeEnd = MIN(1, MAX(0, progressValue));
self.backgroundOvalLayer.strokeEnd = MIN(1, MAX(0, 1 - progressValue));

(. GitHub)

, actionForLayer:forKey: , ( brakepoint).

, ?

-

, UIView , . actionForLayer:(CALayer *)layer forKey:, ( ). .

+4
1

CAShapeLayer strokeStart, strokeEnd fillColor . , , , , shapeLayer CoreAnimation.

, , , , https://gist.github.com/nicklockwood/d374033b27c62662ac8d

+2

Source: https://habr.com/ru/post/1584774/


All Articles