Skip to content

Instantly share code, notes, and snippets.

@chrisallick
Created January 31, 2013 05:18
Show Gist options
  • Save chrisallick/4680452 to your computer and use it in GitHub Desktop.
Save chrisallick/4680452 to your computer and use it in GitHub Desktop.
This is one way to animate the filling of a circle in objective-c, does anyone know the right way?
//
// FillCircle.m
// cramera
//
// Created by Chris Allick on 1/30/13.
// Copyright (c) 2013 Chris Allick. All rights reserved.
//
#import "FillCircle.h"
#define RAD2DEG(radians) ((radians) * (180.0 / M_PI))
#define DEG2RAD(angle) ((angle) / 180.0 * M_PI)
@implementation FillCircle {
float angle;
}
@synthesize checkImageView, checkImage;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
angle = 0.0;
checkImage = [UIImage imageNamed:@"close.png"];
checkImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[checkImageView setImage:checkImage];
[NSTimer scheduledTimerWithTimeInterval:.01
target:self
selector:@selector(tick:)
userInfo:nil
repeats:YES];
}
return self;
}
- (void) tick:(NSTimer *) timer {
if( angle < 1.0 ) {
[self setNeedsDisplay];
angle += .03;
}
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextBeginPath(context);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextMoveToPoint(context, rect.size.width/2,rect.size.height/2);
CGContextAddArc(context, rect.size.width/2,rect.size.height/2, rect.size.width/2, DEG2RAD(0), DEG2RAD(360*angle), 0);
CGContextClosePath(context);
CGContextFillPath(context);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment