我猜你得到错误的原因是因为setPosition方法是因为你试图直接在C4Shape对象上运行方法。本质上,C4Shapes没有这种方法。
在C4中,所有对象都可以附加手势,但为了在对象上触发手势时运行任何自定义代码,您应首先划分C4Shape的子类并编写一个特殊方法来处理您想要查看的行为。
例如,我会创造一个MyShape的阶级是这样的:
@interface MyShape : C4Shape
-(void)setNewPosition;
@end
@implementation MyShape
-(void)setNewPosition {
//will set the center of the shape to a new random point
self.center = CGPointMake([C4Math randomInt:768], [C4Math randomInt:1024]);
}
@end
然后,在我的C4WorkSpace:
#import "C4WorkSpace.h"
#import "MyShape.h"
@implementation C4WorkSpace {
MyShape *newShape;
}
-(void)setup {
newShape = [MyShape new];
[newShape ellipse:CGRectMake(100, 100, 200, 200)];
[newShape addGesture:TAP name:@"tapGesture" action:@"setNewPosition"];
[self.canvas addShape:newShape];
}
@end
这应该跟你的新子类的形状注册敲击手势和运行当手势被触发时,其方法为setNewPosition
。
刚试过这个,它不起作用。在ios模拟器中,我看到的只是一个空白屏幕。 – jamesco 2012-04-19 17:00:21
嘿,对不起,我写了,没有测试它......我更新了上面的答案中的代码,我测试了它,它在我的模拟器中工作。 – 2012-04-19 17:03:59
也适用于我的模拟器!谢谢 – jamesco 2012-04-19 17:05:40