简体   繁体   中英

Remove a UIPanGestureRecognizer after add it to view

我有一个视图,我想使用户能够在使用UIButton标签后在其上进行绘图,我已经使用UIPanGestureRecognizer进行了此操作,在UIButton触摸后在此视图中添加了一个UIPanGestureRecognizer但是问题是我完成绘图后如何删除该UIPanGestureRecognizer并重新触摸UIButton

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

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 ];

  }   
}
UIPanGestureRecognizer gestureRecognizer.cancelsTouchesInView = NO;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM