我在一个C4应用程序的画布上添加了42个形状。如何确定哪个形状已被用户触摸?
我将这些形状添加如下:
#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我只需要知道点击的是哪个号码。
但是我不知道在运行行之后如何访问:[self listenFor:@"touchesBegan" fromObject:greyRect andRunMethod:@"highlightLetter"];
有什么建议吗?
发布于 2013-10-23 18:50:23
查看谁说什么?站点上的通知教程中的C4部分。
通知教程的这一部分解释了如何对给定对象的通知作出反应,并实际确定哪个对象刚刚广播了该通知。
诀窍在于构建一个接受通知的方法:
-(void)highlightLetter:(NSNotification *)notification {
C4Shape *shape = (C4Shape *)notification.object;
//do stuff to the shape
}另外,请记住,由于该方法接受变量,所以必须在方法名称中包括:,如下所示:
[self listenFor:@"touchesBegan"
fromObject:greyRect
andRunMethod:@"highlightLetter:"];https://stackoverflow.com/questions/19539394
复制相似问题