除触摸之外的对象通知*不起作用

时间:2013-07-11 00:41:01

标签: c4

我正在尝试将除touchesBegan以外的通知附加到C4Shape,但我什么都没得到。我添加了C4Shape并尝试在设置底部添加其他通知。问题是,唯一被调用的通知是touchesBegan

我还尝试添加手势la http://www.c4ios.com/examples/listenFor.php但是当我这样做时touchesBegan通知会从该对象停止。

#import "C4WorkSpace.h"

@implementation C4WorkSpace {
    C4Shape * topthing;
}

-(void)setup {    
    C4Font * f = [C4Font fontWithName:@"ArialRoundedMTBold" size:50.0f];

    topthing = [C4Shape shapeFromString:@"touch me" withFont:f];
    [topthing setCenter:CGPointMake(self.canvas.center.x, self.canvas.height/6)];
    [self.canvas addSubview:topthing];
    [topthing setAnimationDuration:2.0];

    [self listenFor:@"touchesBegan" fromObject:topthing andRunMethod:@"bangg:"];
    [self listenFor:@"tapped" fromObject:topthing andRunMethod:@"bangg:"];
    [self listenFor:@"longPress" fromObject:topthing andRunMethod:@"bangg:"];
    [self listenFor:@"swipedRight" fromObject:topthing andRunMethod:@"bangg:"];

    [self runMethod:@"fffff" afterDelay:1.0];
}

-(void) fffff {
    [topthing  rect:CGRectMake(0, 0, self.canvas.width, self.canvas.height/3)];
    [topthing setAnimationDuration:0.0];
}

- (void) bangg:(NSNotification *) notification {
   C4Log(@"%@", [notification name]);
}
@end

1 个答案:

答案 0 :(得分:1)

这里发生了两件事......

1)听取手势广播是不够的。您需要实际将手势添加到形状本身,以便广播。对于您正在聆听的3个手势,您应该使用以下代码:

[topthing addGesture:TAP name:@"tap" action:@"tapped:"];
[topthing addGesture:LONGPRESS name:@"long" action:@"pressedLong"];
[topthing addGesture:SWIPERIGHT name:@"right" action:@"swipedRight:"];

[self listenFor:@"tapped" fromObject:topthing andRunMethod:@"bang1:"];
[self listenFor:@"pressedLong" fromObject:topthing andRunMethod:@"bang2:"];
[self listenFor:@"swipedRight" fromObject:topthing andRunMethod:@"bang3:"];

2)当您向形状添加手势时,预期的行为是关闭touchesBegan。这样做的原因是,您向对象添加LONGPRESS手势,您希望LONGPRESS手势的方法在一定时间后运行。如果你没有关闭touchesBegan那么两种方法都会运行:第一个touchesBegan被触发,然后你的longPress方法被触发,如果你只需要长按方法运行就会很麻烦。同样的逻辑适用于所有手势。

如果您想允许touchesBegan运行,您可以执行以下操作:

[topthing gestureForName:@"tap"].delaysTouchesBegan = NO;
[topthing gestureForName:@"long"].delaysTouchesBegan = NO;
[topthing gestureForName:@"right"].delaysTouchesBegan = NO;

...请注意,必须对对象上的所有手势进行操作。


如果您打算向对象添加复杂的手势功能,我建议您对-(void)tapped{}-(void)swipedRight{}等单个方法进行子类化和重写。这将允许您更轻松地自定义操作,而不是从画布处理它们。


#import "C4WorkSpace.h"

@implementation C4WorkSpace { C4Shape * topthing; }

-(void)setup {
    C4Font * f = [C4Font fontWithName:@"ArialRoundedMTBold" size:50.0f];

    topthing = [C4Shape shapeFromString:@"touch me" withFont:f];
    [topthing setCenter:CGPointMake(self.canvas.center.x, self.canvas.height/6)];
    [self.canvas addSubview:topthing];
    [topthing setAnimationDuration:2.0];

    [topthing addGesture:TAP name:@"tap" action:@"tapped:"];
    [topthing addGesture:LONGPRESS name:@"long" action:@"pressedLong"];
    [topthing addGesture:SWIPERIGHT name:@"right" action:@"swipedRight:"];

    [self listenFor:@"tapped:" fromObject:topthing andRunMethod:@"bang1:"];
    [self listenFor:@"pressedLong" fromObject:topthing andRunMethod:@"bang2:"];
    [self listenFor:@"swipedRight" fromObject:topthing andRunMethod:@"bang3:"];

    [self runMethod:@"fffff" afterDelay:1.0];
}

-(void) fffff {
    [topthing  rect:CGRectMake(0, 0, self.canvas.width, self.canvas.height/3)];
    [topthing setAnimationDuration:0.0];
}

-(void)bang1:(NSNotification *)notification {C4Log(@"%@",[notification name]);}
-(void)bang2:(NSNotification *)notification {C4Log(@"%@",[notification name]);}
-(void)bang3:(NSNotification *)notification {C4Log(@"%@",[notification name]);}
@end