Skip to content

Instantly share code, notes, and snippets.

@C4Examples
Created April 23, 2013 09:08
Show Gist options
  • Save C4Examples/5442016 to your computer and use it in GitHub Desktop.
Save C4Examples/5442016 to your computer and use it in GitHub Desktop.
Advanced Examples arcs
//
// C4WorkSpace.m
// Examples
//
// Created by Travis Kirton.
//
#import "C4WorkSpace.h"
@implementation C4WorkSpace {
C4Shape *arc1;
CGPoint pts[2];
}
-(void)setup {
[self runMethod:@"arcDraw" afterDelay:1.0f];
}
-(void)arcDraw {
int i = [self.canvas.subviews count];
if(i == 0) pts[0] = CGPointMake((CGFloat)[C4Math randomInt:(int)self.canvas.width],self.canvas.center.y);
else pts[0] = pts[1];
pts[1] = CGPointMake((CGFloat)[C4Math randomInt:(int)self.canvas.width], self.canvas.center.y);
BOOL up = (i % 2 == 0) ? YES : NO;
C4Shape *arc = [self arcBetweenPoints:pts up:up];
[self.canvas addShape:arc];
[self runMethod:@"animateArc:" withObject:arc afterDelay:0.0f];
[self runMethod:@"arcDraw" afterDelay:1.0f];
}
-(C4Shape *)arcBetweenPoints:(CGPoint *)beginEndPoints up:(BOOL)up {
CGPoint p1 = beginEndPoints[0];
CGPoint p2 = beginEndPoints[1];
CGFloat radius = [C4Math absf:p2.x - p1.x]/2;
CGPoint center = self.canvas.center;
if(p2.x > p1.x) {
center.x = p1.x + radius;
} else {
center.x = p2.x + radius;
}
CGFloat startAngle;
if(up == YES) startAngle = PI;
else startAngle = 0;
CGFloat endAngle = startAngle + PI;
C4Shape *s = [C4Shape arcWithCenter:center radius:radius
startAngle:startAngle endAngle:endAngle clockwise:YES];
s.fillColor = [UIColor clearColor];
if(p2.x > p1.x) {
if(up) {
s.strokeEnd = 0.0f;
} else {
s.strokeStart = 1.0f;
}
} else {
if(up) {
s.strokeStart = 1.0f;
} else {
s.strokeEnd = 0.0f;
}
}
if(up) s.strokeColor = C4GREY;
return s;
}
-(void)animateArc:(C4Shape *)arc {
arc.animationDuration = 1.0f;
if(arc.strokeStart == 1.0f) arc.strokeStart = 0.0f;
else arc.strokeEnd = 1.0f;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment