UIGestureRecognizerDelegate 实现不起作用
UIGestureRecognizerDelegate implemenation doesn't work
我试图在我的 ViewController 中实现 UIGestureRecognizerDelegate,但不知何故没有调用这些方法。这是控制器:
#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 方法,但是 none 识别器方法。
不要忘记声明识别器并将其添加到要检测点击或滑动的视图
示例:
将 属性 添加到 VC。
分配并初始化识别器:
self.theTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(someMethod:)];
self.theTapRecognizer.delegate = self;
[someView addGestureRecognizer: selftheTapRecognizer];
someView 是您要在其中初始化该识别器的视图的占位符文本,它可以是整个 self.view 或某些子视图,
您可以使用以下委托方法监听与该手势识别器的任何交互
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
假设你已经从故事板中添加了UIGestureRecognizer
,你需要记得CTRL + 拖动手势对象到UIViewController
黄色圆圈图像并设置委托。
您的 UIGestureRecognizer
对象将位于 UIViewController
视图的顶部。 CTL 把这个拖到黄色圆圈ViewController icon
然后此弹出窗口将显示 select 'Delegate' 以便 gesutureRecognizer 将使用委托方法。
我试图在我的 ViewController 中实现 UIGestureRecognizerDelegate,但不知何故没有调用这些方法。这是控制器:
#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 方法,但是 none 识别器方法。
不要忘记声明识别器并将其添加到要检测点击或滑动的视图
示例:
将 属性 添加到 VC。
分配并初始化识别器:
self.theTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(someMethod:)];
self.theTapRecognizer.delegate = self;
[someView addGestureRecognizer: selftheTapRecognizer];
someView 是您要在其中初始化该识别器的视图的占位符文本,它可以是整个 self.view 或某些子视图, 您可以使用以下委托方法监听与该手势识别器的任何交互
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
假设你已经从故事板中添加了UIGestureRecognizer
,你需要记得CTRL + 拖动手势对象到UIViewController
黄色圆圈图像并设置委托。
您的 UIGestureRecognizer
对象将位于 UIViewController
视图的顶部。 CTL 把这个拖到黄色圆圈ViewController icon
然后此弹出窗口将显示 select 'Delegate' 以便 gesutureRecognizer 将使用委托方法。