I have added 42 shapes to the canvas of a C4 app. How can I determine which one of the shapes has been touched by a user?

I add the shapes as follows:

#import "C4Workspace.h"

@implementation C4WorkSpace{
    C4Shape *greyRect;
}

-(void)setup {
    int imageWidth=53.53;
    int imageHeight=65.1;
    for (int i=0; i<42; i++) {
        int xMultiplier=(i)%6;
        int yMultiplier= (i)/6;
        int xPos=xMultiplier*imageWidth;
        int yPos=yMultiplier*imageHeight;
        greyRect=[C4Shape rect:CGRectMake(xPos, yPos, imageWidth, imageHeight)];
        greyRect.fillColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        greyRect.lineWidth=2;
        greyRect.strokeColor=[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1];
        [self listenFor:@"touchesBegan" fromObject:greyRect andRunMethod:@"highlightLetter"];

        [self.canvas addShape:greyRect];
    }
}

-(void)highlightLetter{
    C4Log(@"highlightLetter");
}

@end

I pretty much only need to know which number[i] the clicked rect has.

But I don't know how to access that after running the line: [self listenFor:@"touchesBegan" fromObject:greyRect andRunMethod:@"highlightLetter"];

Any suggestions?

有帮助吗?

解决方案

Have a look at the WHO SAID WHAT? part of the notifications tutorial on C4 site.

This part of the notifications tutorial explains how to react to a given object's notification and actually figure out which object just broadcast that notification.

The trick is in building a method that accepts the notification:

-(void)highlightLetter:(NSNotification *)notification {
    C4Shape *shape = (C4Shape *)notification.object;
    //do stuff to the shape
}

Also, remember that because the method takes a variable you have to include the : in the name of the method like so:

[self listenFor:@"touchesBegan" 
     fromObject:greyRect 
   andRunMethod:@"highlightLetter:"];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top