[英]Remove a UIPanGestureRecognizer after add it to view
我有一个视图,我想使用户能够在使用UIButton
标签后在其上进行绘图,我已经使用UIPanGestureRecognizer
进行了此操作,在UIButton
触摸后在此视图中添加了一个UIPanGestureRecognizer
但是问题是我完成绘图后如何删除该UIPanGestureRecognizer
并重新触摸UIButton
?
UIView has a method called UIView有一个称为
- (void)removeGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
alternatively, you can disable the UIGestureRecognizer temporarily by removing its callback using the method 或者,您可以通过使用方法删除其回调来暂时禁用UIGestureRecognizer
- (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? 什么是objc_setAssociatedObject()?在什么情况下应使用它?
So at the top of your .m file, you'd put 因此,在.m文件的顶部,
static char overviewKey;
then right before you add your UIPanGestureRecognizer
to the view, you'd tag it with a string. 然后在将
UIPanGestureRecognizer
添加到视图之前,先用字符串标记它。
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. 当您想要删除
UIPanGestureRecognizer
,您将浏览该视图中的所有手势识别器,找到带有字符串的手势识别器并将其删除。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.