Okay, the easiest way to do this is to set up your canvas to listen for the right method that is being called inside the C4Shape (actually, it's from any C4Control so this technique will work for all visual objects.
- First, create a shape and add it to the canvas.
- Add a gesture to the shape, triggering the appropriate
swipe
method - Tell the canvas to listen for a
notification
from the shape - Do something (i.e. change the shape's color) when the canvas hears the notification
The following code sets up the shape:
@implementation C4WorkSpace {
C4Shape *s;
}
-(void)setup {
s = [C4Shape rect:CGRectMake(0, 0, 192, 96)];
s.center = self.canvas.center;
[s addGesture:SWIPELEFT name:@"leftSwipeGesture" action:@"swipedLeft"];
[self.canvas addShape:s];
[self listenFor:@"swipedLeft" fromObject:s andRunMethod:@"randomColor"];
}
-(void)randomColor {
s.fillColor = [UIColor colorWithRed:[C4Math randomInt:100]/100.0f
green:[C4Math randomInt:100]/100.0f
blue:[C4Math randomInt:100]/100.0f
alpha:1.0f];
}
@end
However, this is hard-coded... A nicer, more dynamic way of doing this is to listen from a lot of objects and have a randomColor:
method that also accepts the notification so that you can pull out the shape that's doing the notifying.
@implementation C4WorkSpace {
C4Shape *s1, *s2;
}
-(void)setup {
s1 = [C4Shape rect:CGRectMake(0, 0, 192, 96)];
[s1 addGesture:SWIPELEFT name:@"leftSwipeGesture" action:@"swipedLeft"];
s2 = [C4Shape rect:CGRectMake(0, 0, 192, 96)];
[s2 addGesture:SWIPELEFT name:@"left" action:@"swipedLeft"];
s1.center = CGPointMake(self.canvas.center.x, self.canvas.center.y - s1.height * 1.25);
s2.center = CGPointMake(self.canvas.center.x, self.canvas.center.y + s2.height * 0.25);
NSArray *shapes = @[s1,s2];
[self.canvas addObjects:shapes];
[self listenFor:@"swipedLeft" fromObjects:shapes andRunMethod:@"randomColor:"];
}
-(void)randomColor:(NSNotification *)notification {
C4Shape *shape = (C4Shape *)notification.object;
shape.fillColor = [UIColor colorWithRed:[C4Math randomInt:100]/100.0f
green:[C4Math randomInt:100]/100.0f
blue:[C4Math randomInt:100]/100.0f
alpha:1.0f];
}
@end
Things to note in the second example:
First, to accept a notification, the method being run has to have the format:
-(void)randomColor:(NSNotification *)notification {}
Second, to trigger this the method name you use in listenFor
has to have a :
like so:
@"randomColor:"
Third, you grab the object that just received a swipe gesture by pulling it from the notification it sent:
C4Shape *shape = (C4Shape *)notification.object;