UIGestureRecognizerDelegate实现不起作用

时间:2015-04-06 17:31:57

标签: ios objective-c xcode ios8

我试图在我的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方法,但没有调用识别器方法。

2 个答案:

答案 0 :(得分:2)

不要忘记声明并将识别器添加到要检测点击或滑动的视图中

示例:

添加&#34; theTapRecognizer&#34;等属性。到VC。

Alloc和init识别器:

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

答案 1 :(得分:0)

假设您已从故事板添加了UIGestureRecognizer,则需要记住 CTRL +拖动手势对象到UIViewController黄色圆圈图像并设置代理。

您的UIGestureRecognizer对象将位于UIViewController视图的顶部。 CTL将其拖动到黄色圆圈ViewController图标

enter image description here

然后此弹出窗口将显示并选择“委托”,以便gesutureRecognizer将使用委托方法。

enter image description here