1

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

我还尝试添加手势 a 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
4

1 回答 1

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
于 2013-07-11T04:02:06.160 回答