投稿したコードに主な問題が1つあります。
デフォルトでは、C4で表示されているすべてのオブジェクトは、touchesBegan
タップされたときに通知を送信します。あなたのコードではあなたが聞いているの@"touch"
に対し、あなたは聞いて@"touchesBegan"
いるべきものです。
色の変更方法は簡単に実装できます...MyShape.mファイルでは、次のような方法を使用できます。
-(void)changeColor {
CGFloat red = RGBToFloat([C4Math randomInt:255]);
CGFloat green = RGBToFloat([C4Math randomInt:255]);
CGFloat blue = RGBToFloat([C4Math randomInt:255]);
self.fillColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}
物事をうまく機能させるために、C4WorkSpace.mは次のようになります。
#import "C4WorkSpace.h"
#import "MyShape.h"
@implementation C4WorkSpace {
MyShape *s1, *s2;
}
-(void)setup {
s1 = [MyShape new];
s2 = [MyShape new];
[s1 rect:CGRectMake(100, 100, 100, 100)];
[s2 rect:CGRectMake(300, 100, 100, 100)];
[s1 listenFor:@"touchesBegan" fromObject:s2 andRunMethod:@"changeColor"];
[s2 listenFor:@"touchesBegan" fromObject:s1 andRunMethod:@"changeColor"];
[self.canvas addShape:s1];
[self.canvas addShape:s2];
}
@end