Search code examples
objective-cioseventsuigesturerecognizerquicklook

Reading touch events in a QLPreviewController


I've got a QuickLook view that I view some of my app's documents in. It works fine, but I'm having my share of trouble closing the view again. How do I create a touch event / gesture recognizer for which I can detect when the user wants to close the view?

I tried the following, but no events seem to trigger when I test it.

/------------------------ [ TouchPreviewController.h ]---------------------------
#import <Quicklook/Quicklook.h>

@interface TouchPreviewController : QLPreviewController

@end

//------------------------ [ TouchPreviewController.m ]---------------------------
#import "TouchPreviewController.h"

@implementation TouchPreviewController

- (id)init:(CGRect)aRect {
    if (self = [super init]) {
        // We set it here directly for convenience
        // As by default for a UIImageView it is set to NO
        UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc]
                                                initWithTarget:self action:@selector(handleSingleDoubleTap:)];
        singleFingerDTap.numberOfTapsRequired = 2;
        [self.view addGestureRecognizer:singleFingerDTap];
        [self.view setUserInteractionEnabled:YES];
        [self.view setMultipleTouchEnabled:YES];
        //[singleFingerDTap release];
    }
    return self;
}

- (IBAction)handleSingleDoubleTap:(UIGestureRecognizer *) sender {
    CGPoint tapPoint = [sender locationInView:sender.view.superview];
    [UIView beginAnimations:nil context:NULL];
    sender.view.center = tapPoint;
    [UIView commitAnimations];

    NSLog(@"TouchPreviewController tap!" ) ;
}

// I also tried adding this
- (BOOL)gestureRecognizer:(UIGestureRecognizer *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*) otherGestureRecognizer {
    return YES;
}

@end

Edit: For clarification, this is how I instantiate the controller:

documents = [[NSArray alloc] initWithObjects: filename , nil ] ;

preview = [[TouchPreviewController alloc] init];
preview.dataSource = self;
preview.delegate = self;

//set the frame from the parent view
CGFloat w= backgroundViewHolder.frame.size.width; 
CGFloat h= backgroundViewHolder.frame.size.height;
preview.view.frame = CGRectMake(0, 0,w, h);

//refresh the preview controller
[preview reloadData];
[[preview view] setNeedsLayout];
[[preview view] setNeedsDisplay];
[preview refreshCurrentPreviewItem];

//add it  
[quickLookView addSubview:preview.view];

Also, I've defined the callback methods as this:

- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
    return [documents count];
}

- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index
{
    return [NSURL fileURLWithPath:[documents objectAtIndex:index]];
}

Edit2: One thing i noticed. If I try making swiping gestures, I get the following message. This could shed some light on what is wrong/missing?

Ignoring call to [UIPanGestureRecognizer setTranslation:inView:] since gesture recognizer is not active.


Solution

  • I think your example code is incomplete. It isn't clear how you are instantiating the TouchPreviewController (storyboard, nib file or loadView.)

    I have never used the class so I could be way out in left field.

    If you've already instantiated a UITapGestureRecognizer in the parent viewController, it is absorbing the tap events and they aren't passed on to your TouchPreviewController.

    I would implement the view hierarchy differently by attaching the UITapGestureRecognizer to the parent viewController and handle presentation and unloading of the QLPreviewController there.

    I think you might not have to subclass QLPreviewController by instantiating the viewController from a nib file.

    When your parent viewController's UITapGestureRecognizer got an event you would either push the QLPreviewController on the navigation stack or pop it off the navigation stack when done.

    Hope this is of some help.