AD AD
AD AD

Reputation: 53

Remove a UIPanGestureRecognizer after add it to view

I have a view and I want to enable a user to draw on it after tab a UIButton, I have do this usingUIPanGestureRecognizer , add a UIPanGestureRecognizer this view after UIButton touch but the issue is how I can remove this UIPanGestureRecognizer after I done my drawing and re-touch the UIButton ??

Upvotes: 0

Views: 2025

Answers (3)

Rog182
Rog182

Reputation: 4879

If you have multiple pan gesture recognizers for a view, you can tag a specific one with an associated object.

What is objc_setAssociatedObject() and in what cases should it be used?

So at the top of your .m file, you'd put

static char overviewKey;

then right before you add your UIPanGestureRecognizer to the view, you'd tag it with a string.

objc_setAssociatedObject(panGesture, &overviewKey, @"pan gesture for drawing", OBJC_ASSOCIATION_RETAIN_NONATOMIC);

[someView addGestureRecognizer:panGesture];

When you want to delete the UIPanGestureRecognizer, you'd go through all the gesture recognizers in that view, find the one with the string, and delete it.

for (UIGestureRecognizer *gesture in someView) {

  NSString *gestureTag= objc_getAssociatedObject(gesture, &overviewKey);


  if (gestureTag==nil) {

    continue;

   }

  if ([gestureTag isEqual:@"pan gesture for drawing"]) {

    [ someView removeGestureRecognizer:gesture ];

  }   
}

Upvotes: 0

Kattu Poochi
Kattu Poochi

Reputation: 139

UIPanGestureRecognizer gestureRecognizer.cancelsTouchesInView = NO;

Upvotes: 0

jbat100
jbat100

Reputation: 16827

UIView has a method called

- (void)removeGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer

alternatively, you can disable the UIGestureRecognizer temporarily by removing its callback using the method

- (void)removeTarget:(id)target action:(SEL)action

Upvotes: 3

Related Questions