投稿したコードに大きな問題が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はなっているはずですlike:
#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
私は答える前にヘビーデイヴィッド、2つの質問:(1) "ashape"と "anothershape"クラスMyShapeの両方のオブジェクトですか? (2)touchesBeganメソッドを使って最初に触れたときに、オブジェクトの1つを反応させようとしていますか? –
1)はい 2)はい。たとえば:私は四角形にする必要がある場合、私は最初の1つを押すと、2番目の正方形の色を変更したいとviceversa。 – davidpenuela