首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C4点击了哪个形状?

C4点击了哪个形状?
EN

Stack Overflow用户
提问于 2013-10-23 10:39:12
回答 1查看 24关注 0票数 1

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

我将这些形状添加如下:

代码语言:javascript
复制
#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"];

有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-23 18:50:23

查看谁说什么?站点上的通知教程中的C4部分。

通知教程的这一部分解释了如何对给定对象的通知作出反应,并实际确定哪个对象刚刚广播了该通知。

诀窍在于构建一个接受通知的方法:

代码语言:javascript
复制
-(void)highlightLetter:(NSNotification *)notification {
    C4Shape *shape = (C4Shape *)notification.object;
    //do stuff to the shape
}

另外,请记住,由于该方法接受变量,所以必须在方法名称中包括:,如下所示:

代码语言:javascript
复制
[self listenFor:@"touchesBegan" 
     fromObject:greyRect 
   andRunMethod:@"highlightLetter:"];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19539394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档