【问题标题】:C4 which shape was clicked?C4 点击了哪个形状?
【发布时间】:2013-10-23 10:39:12
【问题描述】:

我在 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

我几乎只需要知道点击的矩形有哪个数字[i]。

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

有什么建议吗?

【问题讨论】:

    标签: touch interaction c4


    【解决方案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:"];
    

    【讨论】:

    • 这就是诀窍,我按照我需要的方式对其进行了调整!谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多