Skip to content

Instantly share code, notes, and snippets.

@C4Examples
Created July 24, 2012 21:19
Show Gist options
  • Save C4Examples/3172723 to your computer and use it in GitHub Desktop.
Save C4Examples/3172723 to your computer and use it in GitHub Desktop.
Advanced Curves (with drag interaction)
//
// C4WorkSpace.m
// Examples
//
// Created by Travis Kirton on 12-07-19.
//
#import "C4WorkSpace.h"
@interface C4WorkSpace ()
-(void)updateControlA;
-(void)updateControlB;
-(void)updateControlC;
@end
@implementation C4WorkSpace {
C4Shape *bezierCurve, *quadraticCurve;
C4Shape *controlA, *controlB, *controlC;
}
-(void)setup {
//create the end points for the bezier curve
CGPoint bezierEndPoints[2] = {
CGPointMake(0.3f*self.canvas.width,self.canvas.height/3),
CGPointMake(0.7f*self.canvas.width,self.canvas.height/3)
};
//create the control points for the bezier curve
CGPoint bezierControlPoints[2] = {CGPointMake(32,32), CGPointMake(self.canvas.width-32,32)};
//create the bezier curve
bezierCurve = [C4Shape curve:bezierEndPoints controlPoints:bezierControlPoints];
[self.canvas addShape:bezierCurve];
//create the end points for the bezier curve
CGPoint quadEndPoints[2] = {
CGPointMake(0.3f*self.canvas.width,self.canvas.height*2/3),
CGPointMake(0.7f*self.canvas.width,self.canvas.height*2/3)
};
//create the control point for the quadratic curve
CGPoint quadControlPoint = self.canvas.center;
quadControlPoint.y = self.canvas.height - 32;
//create the bezier curve
quadraticCurve = [C4Shape quadCurve:quadEndPoints controlPoint:quadControlPoint];
[self.canvas addShape:quadraticCurve];
//create 3 control shapes
CGRect controlFrame = CGRectMake(0, 0, 44, 44);
controlA = [C4Shape ellipse:controlFrame];
controlB = [C4Shape ellipse:controlFrame];
controlC = [C4Shape ellipse:controlFrame];
//move control shapes points to the coordinates of the curve control points
controlA.center = bezierControlPoints[0];
controlB.center = bezierControlPoints[1];
controlC.center = quadControlPoint;
//style the control shapes
controlA.lineWidth = controlB.lineWidth = controlC.lineWidth = 0.0f;
controlB.fillColor = C4RED;
controlC.fillColor = C4GREY;
[self.canvas addShape:controlA];
[self.canvas addShape:controlB];
[self.canvas addShape:controlC];
//add drag gestures to the control shapes
[controlA addGesture:PAN name:@"panGesture" action:@"move:"];
[controlB addGesture:PAN name:@"panGesture" action:@"move:"];
[controlC addGesture:PAN name:@"panGesture" action:@"move:"];
//listen for when the control shapes are moved and update the curves as needed
[self listenFor:@"moved" fromObject:controlA andRunMethod:@"updateControlA"];
[self listenFor:@"moved" fromObject:controlB andRunMethod:@"updateControlB"];
[self listenFor:@"moved" fromObject:controlC andRunMethod:@"updateControlC"];
}
-(void)updateControlA {
bezierCurve.controlPointA = controlA.center;
}
-(void)updateControlB {
bezierCurve.controlPointB = controlB.center;
}
-(void)updateControlC {
quadraticCurve.controlPointA = controlC.center;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment