Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable GestureRecognizer

I have a simple memory game. But i want to turn off the tap function.

When I use imageViewGurka1.gestureRecognizers=NO; it works, but I get the warning message: Initialization of pointer of type 'NSArray *' to null from a constant boolean expression. (what should I do to fix this warning message?)

And if I use this imageViewGurka1 setUserInteractionEnable:NO; I don't get a warning message, but I will not be able to move the image any more.

Here is some of the code.

-(IBAction)handleTapGurka1:(UIGestureRecognizer *)sender {

if (imageViewGurka1.tag == 0) {
    [imageViewGurka1 setImage:[UIImage imageNamed:@"memorySångerGurka.png"]];
    imageViewGurka1.tag=1;
    [self.view bringSubviewToFront:imageViewGurka1];

}

else {
    [imageViewGurka1 setImage:[UIImage imageNamed:@"memorySångerBaksida.png"]];
    imageViewGurka1.tag=0;
    [self.view bringSubviewToFront:imageViewGurka1];
}

if (imageViewGurka1.tag==1 && imageViewGurka2.tag==1) {
    NSURL *musicFile;
    musicFile = [NSURL fileURLWithPath:
                 [[NSBundle mainBundle]
                  pathForResource:@"applader"
                  ofType:@"mp3"]];
    myAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
    [myAudio play];
    //[imageViewGurka1 setUserInteractionEnabled:NO];
    //[imageViewGurka2 setUserInteractionEnabled:NO];
    imageViewGurka1.gestureRecognizers=NO;
    imageViewGurka2.gestureRecognizers=NO;
}

}

Thanks for any help!

Regards

like image 606
user3266053 Avatar asked Feb 10 '15 22:02

user3266053


2 Answers

gestureRecognizers is an array that contains all gesture recognizers attached to the view. You should loop through all gesture recognizers in that array and set their enabled property to false like this:

for (UIGestureRecognizer * g in imageViewGurka1.gestureRecognizers) {
    g.enabled = NO;
}

Swift 5

Below the equivalent for Swift 5

for gesture in imageViewGurka1.gestureRecognizers! {
    gesture.isEnabled = false
}
like image 100
Cihan Tek Avatar answered Oct 24 '22 14:10

Cihan Tek


At swift4,

Well in case of many gestures on many views you can enable and disable a gesture like this.

Lets say we have UIView array and we want to disable a gesture which is the first gesture added to the first view in view array named as ourView.

   ourView[0].gestureRecognizers![0].isEnabled = false

Also you can enable or disable all the gestures at the same time like this.

   for k in 0..<ourView.count {
      for l in 0..<outView[k].gestureRecognizers.count {
         ourView[k].gestureRecognizers![l].isEnabled = false 
      }
   }
like image 43
Hope Avatar answered Oct 24 '22 14:10

Hope