Domanda

Reading through the documentation here.

I know how to successfully setup PAN gestures for a C4Object. How would I disable a PAN gesture though?

Using...

[object setUserInteractionEnabled:NO]

... disables all gestures including TAP events and...

object.gestureRecognizers = NO

... doesn't allow me to reinitialize PAN gestures.

If anyone could share with me how disable PAN gestures (toggle PAN on/off) without effecting other gesture events it would be greatly appreciated.

È stato utile?

Soluzione

You can get access to the gestures that you add to an object by using the gestureForName: method, which returns a UIGestureRecognizer object. From there, you can interact with that gesture recognizer and change its properties directly.

To toggle on/off a gesture recognizer, all you have to do is change the value of its enabled property.

The following works for me:

#import "C4WorkSpace.h"

@implementation C4WorkSpace {
    UIGestureRecognizer *gesture;
    C4Shape *square, *circle;
}

-(void)setup {
    square = [C4Shape rect:CGRectMake(0, 0, 100, 100)];
    square.center = self.canvas.center;

    circle = [C4Shape ellipse:square.frame];
    circle.center = CGPointMake(square.center.x, square.center.y + 200);
    [self listenFor:@"touchesBegan" fromObject:circle andRunMethod:@"toggle"];

    [self.canvas addObjects:@[square, circle]];

    [square addGesture:PAN name:@"thePan" action:@"move:"];
    gesture = [square gestureForName:@"thePan"];
}

-(void)toggle {
    gesture.enabled = !gesture.isEnabled;
    if(gesture.enabled == YES) square.fillColor = C4GREY;
    else square.fillColor = C4RED;
}

@end

The key part of this example is the following:

    [square addGesture:PAN name:@"thePan" action:@"move:"];
    gesture = [square gestureForName:@"thePan"];

Notice, in the implementation there is a UIGestureRecognizer variable called gesture. What we do on the second line is find the PAN gesture associated with the square object and keep a reference to it.

Then, whenever we toggle by touching the circle we do the following:

gesture.enabled = !gesture.isEnabled;

That is, if the gesture is enabled then disable it (and vice-versa).

You can check out more on the UIGestureRecognizer Class Reference

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top