好了,要做到这一点,最简单的方法是设置画布监听正被称为C4Shape(实际上,它从任何C4Control是那么这种技术将所有可视对象的工作中正确的方法。
- 首先,创建一个形状,并将其添加到画布上。
- 手势添加到形状,触发相应的
swipe
方法
- 告诉画布,从形状侦听
notification
- 做一些事物(即改变形状的颜色),当画布听到通知
下面的代码设置形状:
@implementation C4WorkSpace {
C4Shape *s;
}
-(void)setup {
s = [C4Shape rect:CGRectMake(0, 0, 192, 96)];
s.center = self.canvas.center;
[s addGesture:SWIPELEFT name:@"leftSwipeGesture" action:@"swipedLeft"];
[self.canvas addShape:s];
[self listenFor:@"swipedLeft" fromObject:s andRunMethod:@"randomColor"];
}
-(void)randomColor {
s.fillColor = [UIColor colorWithRed:[C4Math randomInt:100]/100.0f
green:[C4Math randomInt:100]/100.0f
blue:[C4Math randomInt:100]/100.0f
alpha:1.0f];
}
@end
然而,这是硬编码...一个更好的,更具活力的做的方式这是从很多对象收听,并有一个方法也接受通知,以便您可以拉出正在进行通知的形状。
@implementation C4WorkSpace {
C4Shape *s1, *s2;
}
-(void)setup {
s1 = [C4Shape rect:CGRectMake(0, 0, 192, 96)];
[s1 addGesture:SWIPELEFT name:@"leftSwipeGesture" action:@"swipedLeft"];
s2 = [C4Shape rect:CGRectMake(0, 0, 192, 96)];
[s2 addGesture:SWIPELEFT name:@"left" action:@"swipedLeft"];
s1.center = CGPointMake(self.canvas.center.x, self.canvas.center.y - s1.height * 1.25);
s2.center = CGPointMake(self.canvas.center.x, self.canvas.center.y + s2.height * 0.25);
NSArray *shapes = @[s1,s2];
[self.canvas addObjects:shapes];
[self listenFor:@"swipedLeft" fromObjects:shapes andRunMethod:@"randomColor:"];
}
-(void)randomColor:(NSNotification *)notification {
C4Shape *shape = (C4Shape *)notification.object;
shape.fillColor = [UIColor colorWithRed:[C4Math randomInt:100]/100.0f
green:[C4Math randomInt:100]/100.0f
blue:[C4Math randomInt:100]/100.0f
alpha:1.0f];
}
@end
相近的第二个例子说明:
首先,接受通知,该方法的运行必须有以下格式:
-(void)randomColor:(NSNotification *)notification {}
其次,触发此方法您使用的名称listenFor
已将设置为:
,如下所示:
@"randomColor:"
第三,你抢,只是从它发出的通知,拉它收到轻扫手势对象:
C4Shape *shape = (C4Shape *)notification.object;
取决于...你想从里面的造型触发这个?或者,从工作区本身? – 2013-03-08 03:48:25
@ C4-Travis我不太确定如何说出我想要的东西。当我在形状上做一个滑动手势(不是画布)时,我想让该方法运行。 – 2013-03-08 04:01:23
好的,所以你在形状上运行手势,然后:A)在形状内触发一个方法?或B)在画布上触发一个方法? – 2013-03-08 04:41:56