0

I have a UIView as the main view and add a QLPreviewController as the subview over that while previewing the document. I want to restrict the long press gesture so that no one can copy contents from the document. I have tried the following code:

Code Snippet :

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:nil]; 

longPress.allowableMovement=100; 

longPress.minimumPressDuration=0.3; 
longPress.delegate=self; 
longPress.delaysTouchesBegan=YES;
longPress.delaysTouchesEnded=YES;

longPress.cancelsTouchesInView=YES; 
[previewController.view addGestureRecognizer:longPress]; 
[self.view addSubview:previewController.view];

But no success. Can anyone tell me where am I going wrong and what could be done to disable the long press gesture ?

I have tried this as well :

NSArray *arr = previewController.view.gestureRecognizers;

for (int i = 0; i < arr.count; i++) {

     if ([[arr objectAtIndex:i] isKindOfClass:[UILongPressGestureRecognizer class]]) {

         [previewController.view removeGestureRecognizer:[arr objectAtIndex:i]];
     }
}
batman
  • 1,937
  • 2
  • 22
  • 41
AnxiousMan
  • 578
  • 2
  • 8
  • 25

1 Answers1

0

You can do something like,

NSArray *arr = qlPreviewController.gestureRecognizers;

for (int i = 0; i < arr.count; i++) {

    if ([[arr objectAtIndex:i] isKindOfClass:[UILongPressGestureRecognizer class]]) {

        [qlPreviewController removeGestureRecognizer:[arr objectAtIndex:i]];
    }
}

qlPreviewController is the object of QLPreviewController's view on wich UILongPressGestureRecognizer is there!

qlPreviewController is view not viewcontroller make sure that!

Update :

for example;

  QLPreviewController *vc;  

  UIView *qlPreviewController = vc.view;

Update 2:

you can use this delegate method to disable gesture recognizer!

  - (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{

    return NO;


 }
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • Let me try .. And am sure it isn't a view controller – AnxiousMan Sep 08 '16 at 10:11
  • Can u tell me what do u mean by qlPreviewController is the object of QLPreviewController's view ? – AnxiousMan Sep 08 '16 at 10:14
  • that means the main view of `QLPreviewController` because we adds gesture recognizer on `view` not on viewcontroller!! got it? – Ketan Parmar Sep 08 '16 at 10:16
  • and I get an error "No Visible @interface for 'QLPreviewController' declares the selector for 'removeGestureRecognizer' " – AnxiousMan Sep 08 '16 at 10:16
  • you need to remove gesture recognizer from `previewController.view`. and you got error because you doing same mistake of which i was afraid. You are trying to get array of recognizer from viewcontroller rather than view!!! – Ketan Parmar Sep 08 '16 at 10:20
  • i have update your question now try this one. you was removing gesture recognizer from `previewController` actually you need to remove from `previewController.view` – Ketan Parmar Sep 08 '16 at 10:22
  • Understood.. I ll try now and get back to u'' – AnxiousMan Sep 08 '16 at 10:23
  • you need to remove long press gesture from `QLPreviewController`'s view!! If that's not work then you can implement delegate method `shouldReceiveTouch` to disable it! – Ketan Parmar Sep 08 '16 at 10:31
  • I am adding a long press gesture to the qlpreviewcontroller's view with csncelsTouchInView set to NO.. Why SHould I remove it ? – AnxiousMan Sep 08 '16 at 10:38