我已尝试在我的 ViewController 中实现 UIGestureRecognizerDelegate,但不知何故未调用这些方法。这是 Controller :
#import "DiaryEntryViewController.h"
#import "UINavigationController+BarManagement.h"
@interface DiaryEntryViewController ()<UIGestureRecognizerDelegate>
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@end
@implementation DiaryEntryViewController
-(void)viewWillAppear:(BOOL)inAnimated{
[super viewWillAppear:inAnimated];
self.navigationController.barsHidden = NO;
}
-(void)viewDidAppear:(BOOL)inAnimated{
[super viewDidAppear:inAnimated];
[self.navigationController hideBarsWithDelay:2.0];
}
-(void)viewWillDisappear:(BOOL)inAnimated{
[self.navigationController setBarsHidden:NO animated:YES];
[super viewWillDisappear:inAnimated];
}
-(NSManagedObjectContext *)managedObjectContext{
return self.diaryEntry.managedObjectContext;
}
-(BOOL)saveDiaryEntry{
BOOL theResult = NO;
NSError *theError = nil;
theResult = [self.managedObjectContext save:&theError];
if(!theResult){
NSLog(@"saveItem %@", theError);
}
return theResult;
}
-(CGRect)visibleBounds{
CGRect theBounds = self.view.bounds;
if([self respondsToSelector:@selector(topLayoutGuide)] && [self respondsToSelector:@selector(bottomLayoutGuide)]){
theBounds.origin.y = [self.topLayoutGuide length];
theBounds.size.height -= [self.topLayoutGuide length] + [self.bottomLayoutGuide length];
}
return theBounds;
}
-(IBAction)toogleBars:(id)sender{
NSLog(@"toogleBars");
UINavigationController *theController = self.navigationController;
BOOL theHiddenFlag = theController.barsHidden;
[theController setBarsHidden:!theHiddenFlag animated:YES];
if(theHiddenFlag){
[theController hideBarsWithDelay:2.0];
}
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)inRecognizer{
NSLog(@"gestureRecognizerShouldBegin");
UIView *theView = self.textView;
CGPoint thePoint = [inRecognizer locationInView:theView];
return !CGRectContainsPoint(theView.bounds, thePoint);
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
NSLog(@"bla");
return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
NSLog(@"ble");
return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
NSLog(@"blä");
return YES;
}
@end
它确实调用了 toogleBars 方法,但没有调用任何识别器方法。
最佳答案
不要忘记声明识别器并将其添加到要检测点击或滑动的 View
例子:
向 VC 添加一个属性,如“theTapRecognizer”。
分配并初始化识别器:
self.theTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(someMethod:)];
self.theTapRecognizer.delegate = self;
[someView addGestureRecognizer: selftheTapRecognizer];
someView 是你想要在其中初始化识别器的 View 的占位符文本,它可以是整个 self.view 或一些 subview , 您可以使用以下委托(delegate)方法收听与该手势识别器的任何交互
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
关于ios - UIGestureRecognizerDelegate 实现不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29476295/