另一种解决方案,仅用于历史。您可以使任何视图成为一些手势识别器断路器,它应该在该视图的矩形中工作。必须有另一个带有委托的 UIPanGestureRecognizer。它可以是具有一种方法的任何对象:
static UIPageViewController* getPageViewControllerFromView(UIView* v) {
UIResponder* r = v.nextResponder;
while (r) {
if ([r isKindOfClass:UIPageViewController.class])
return (UIPageViewController*)r;
r = r.nextResponder;
}
return nil;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (getPageViewControllerFromView(otherGestureRecognizer.view))
{
otherGestureRecognizer.enabled = NO;
otherGestureRecognizer.enabled = YES;
}
return NO;
}
您可以将以下类用于识别器破坏目的:
@interface GestureRecognizerBreaker : NSObject <UIGestureRecognizerDelegate>
{
UIGestureRecognizer* breaker_;
BOOL(^needsBreak_)(UIGestureRecognizer*);
}
- (id) initWithBreakerClass:(Class)recognizerClass
checker:(BOOL(^)(UIGestureRecognizer* recognizer))needsBreak;
- (void) lockForView:(UIView*)view;
- (void) unlockForView:(UIView*)view;
@end
@implementation GestureRecognizerBreaker
- (void) dummy:(id)r {}
- (id) initWithBreakerClass:(Class)recognizerClass checker:(BOOL(^)(UIGestureRecognizer*))needsBreak {
self = [super init];
if (!self)
return nil;
NSParameterAssert([recognizerClass isSubclassOfClass:UIGestureRecognizer.class] && needsBreak);
needsBreak_ = needsBreak;
breaker_ = [[recognizerClass alloc] initWithTarget:self action:@selector(dummy:)];
breaker_.delegate = self;
return self;
}
- (BOOL) gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer {
if (needsBreak_(otherGestureRecognizer)) {
otherGestureRecognizer.enabled = NO;
otherGestureRecognizer.enabled = YES;
}
return NO;
}
- (void) lockForView:(UIView*)view {
[view addGestureRecognizer:breaker_];
}
- (void) unlockForView:(UIView*)view {
[view removeGestureRecognizer:breaker_];
}
@end
例如,这可以作为单音:
static GestureRecognizerBreaker* PageViewControllerLocker() {
static GestureRecognizerBreaker* i = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
i = [[GestureRecognizerBreaker alloc]
initWithBreakerClass:UIPanGestureRecognizer.class
checker:^BOOL(UIGestureRecognizer* recognizer) {
UIView* v = recognizer.view;
UIResponder* r = v.nextResponder;
while (r) {
if ([r isKindOfClass:UIPageViewController.class])
return YES;
r = r.nextResponder;
}
return NO;
}];
});
return i;
}
调用 -lockForView: 页面控制器的手势在拖入视图框架后不起作用。例如,我想锁定我的视图控制器的整个空间。所以在视图控制器方法的某个时刻我调用
[PageViewControllerLocker() lockForView:self.view];
在另一点
[PageViewControllerLocker() unlockForView:self.view];