1

I've created a circular loader (for learning purposes) which shows in a percentage how much work is done. This is more a learning experience than actual usage (I know that some libs on git do this thing already). But now I want to animate the change in percentage instead of doing this instantly which is how I do it currently.

This is my code:

- (instancetype)initWithFrame:(CGRect)frame
    {
        if (self == [super initWithFrame:frame]) {
            _percent = 0.0f;
        }
        return self;
    }

    - (void)drawRect:(CGRect)rect
    {
        [self drawLoaderWithFrame:rect percent:_percent];
    }

    - (void) setPercent:(CGFloat)percent {
        _percent = percent / 100.f;
        [self setNeedsDisplay];
    }

    - (void)drawLoaderWithFrame:(CGRect)frame percent:(CGFloat)percent;
    {
        //// Color Declarations
        UIColor* front = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 1];

        //// Variable Declarations
        CGFloat angle = -360 * percent;

        if (_percent != 0.f) {
            CGRect oval2Rect = CGRectMake(CGRectGetMinX(frame) + floor(CGRectGetWidth(frame) * 0.11000 + 0.5), CGRectGetMinY(frame) + floor((CGRectGetHeight(frame) - 78) * 0.50000 + 0.5), floor(CGRectGetWidth(frame) * 0.89000 + 0.5) - floor(CGRectGetWidth(frame) * 0.11000 + 0.5), 78);
            UIBezierPath* oval2Path = UIBezierPath.bezierPath;
            [oval2Path addArcWithCenter: CGPointMake(CGRectGetMidX(oval2Rect), CGRectGetMidY(oval2Rect)) radius: CGRectGetWidth(oval2Rect) / 2 startAngle: 270 * M_PI/180 endAngle: -(angle + 90) * M_PI/180 clockwise: YES];
            [oval2Path addLineToPoint: CGPointMake(CGRectGetMidX(oval2Rect), CGRectGetMidY(oval2Rect))];
            [oval2Path closePath];

            [front setFill];
            [oval2Path fill];
        }
    }

I tried it using a while loop with delay but no luck it still does it instantly.

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.