2013-10-23 37 views
1

我在C4应用程序的画布上添加了42个形状。我如何确定用户触摸了哪一个形状?C4点击了哪个形状?

我添加形状如下:

#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]点击的矩形了。

但我不知道如何访问运行该行之后:[self listenFor:@"touchesBegan" fromObject:greyRect andRunMethod:@"highlightLetter"];

有什么建议?

回答

1

查看C4网站上的通知教程的WHO SAID WHAT?部分。

通知教程的这一部分介绍了如何对给定对象的通知做出反应,并实际找出哪个对象只是广播了该通知。

关键是建立一个接收通知的方法:

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

此外,请记住因为该方法需要一个变量你必须包括在像法的名称:所以:

[self listenFor:@"touchesBegan" 
    fromObject:greyRect 
    andRunMethod:@"highlightLetter:"]; 
+0

这是做的伎俩,我适应了我需要的方式!谢谢!! – suMi