user3266053
user3266053

Reputation: 175

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

Upvotes: 11

Views: 16375

Answers (4)

user3274549
user3274549

Reputation: 71

Based on the Apple's Documentation, you could also use:

func removeTarget(Any?, action: Selector?)

Upvotes: 0

Cihan Tek
Cihan Tek

Reputation: 5409

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
}

Upvotes: 16

Hope
Hope

Reputation: 2326

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 
      }
   }

Upvotes: 6

Derek Soike
Derek Soike

Reputation: 11680

Storyboard/Outlet Solution

Background: I had a UIView that I wanted to act like a button so I added a tap gesture recognizer to it. When a user taps the 'button' I wanted to disable the button, perform some animation, and re-enable the button after the animation.

Solution: If you're using storyboards and you added a gesture recognizer to a UIView, control+drag from the gesture recognizer into the assistant editor to create an outlet. Then you can set gestureRecognizerName.isEnabled = false.

@IBOutlet var customButtonGestureRecognizer: UITapGestureRecognizer!

@IBAction func didTapCustomButton(_ sender: Any) {
    customButtonGestureRecognizer.isEnabled = false
    performSomeAnimation(completionHandler: {
        self.customButtonGestureRecognizer.isEnabled = true
    })
}

enter image description here

Upvotes: 4

Related Questions