How do i call a method from C4Workspace by using [object addGesture…]?

蹲街弑〆低调 提交于 2019-12-06 11:11:25

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.

  1. First, create a shape and add it to the canvas.
  2. Add a gesture to the shape, triggering the appropriate swipe method
  3. Tell the canvas to listen for a notification from the shape
  4. 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;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!