ios - UIGestureRecognizerDelegate 实现不起作用

标签 ios objective-c xcode ios8

我已尝试在我的 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/

相关文章:

ios - 当我使用几种不同的方式显示单元格时,是否可以在 UITableViewCell 中有不同的高度?

objective-c - Doxygen 无法在 Objective-C 中检测到 NS_ENUM

ios - Apple Mach-O 链接器错误 Siri 目标意图扩展添加

xcode - Xcode 中的 Storyboard太慢了

iphone - 解散到 RootViewController?

ios - UIPanGestureRecognizer 在触摸时立即做一些事情

ios - 滑动和旋转 UIView

ios - iOS Swift 中的 Android 服务等价物

objective-c - NSMutableArray获取[__NSCFString计数]:无法识别的选择器已发送到实例

ios - 如何在两个类之间传递变量