Pregunta

I am trying to attach notifications other than touchesBegan to a C4Shape and I am getting nothing. I have added a C4Shape and have tried to add other notifications at the bottom of setup. The problem is that the only notification that ever gets called is touchesBegan.

I also tried adding gestures a la http://www.c4ios.com/examples/listenFor.php but when I do that touchesBegan notifications stop from that object.

#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
¿Fue útil?

Solución

Two things happening here...

1) Listening for gesture broadcasts isn't enough. You need to actually add the gestures to the shape itself so that it will broadcast. For the 3 gestures you're listening for you should use the following code:

[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) When you add gestures to a shape the expected behaviour is to turn off touchesBegan. The reasoning for this is in the case where you add a LONGPRESS gesture to an object you would expect the method for your LONGPRESS gesture to run after a certain amount of time. If you don't turn off touchesBegan then BOTH methods run: first touchesBegan gets triggered, then your longPress method gets triggered which can be troublesome if you want ONLY the long press method to run. This same logic applies to all gestures.

If you want to allow touchesBegan to run you can do the following:

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

...note that it has to be done for all gestures active on the object.


If you plan to add complex gesture functionality to your objects, I would suggest subclassing and overriding the individual methods like -(void)tapped{} and -(void)swipedRight{}. This will allow you to more easily customize actions rather than dealing with them from the canvas.


#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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top