11

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

fguchelaar
  • 4,779
  • 23
  • 36
user3266053
  • 175
  • 1
  • 1
  • 10
  • `gestureRecognizers` is not a boolean (as the error states), and no attempt has been made to consult the documentation on UIGestureRecognizer, where the answer is quite plain. – matt Feb 10 '15 at 22:02

4 Answers4

16

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
}
Gius
  • 89
  • 1
  • 1
  • 11
Cihan Tek
  • 5,349
  • 3
  • 22
  • 29
  • if i put that code after [myAudio play]; i get the error message: Expected '(' after for' ... where should i put the "( )" Thanks for your help! – user3266053 Feb 10 '15 at 22:21
  • Ah, sorry for the mistake. I wrote the answer in Swift, not Obj-C. I've changed it to Obj-C now. Buy if you can't tell the difference, you should study the language first before trying to develop apps. – Cihan Tek Feb 10 '15 at 22:30
  • Thanks for your help. I did do like this. for (UIGestureRecognizer *Gurka1 in imageViewGurka1.gestureRecognizers) { Gurka1.enabled = NO; } for (UIGestureRecognizer *Gurka2 in imageViewGurka2.gestureRecognizers) { Gurka2.enabled = NO; } – user3266053 Feb 10 '15 at 22:40
  • That's right. I forgot the * operator. That's what happens when you keep switching between two different languages :) Please accept the answer. – Cihan Tek Feb 10 '15 at 22:44
  • Accept the answer? Where do i do that? – user3266053 Feb 10 '15 at 22:57
  • There is a checkmark (tick) icon on the left side of this post, just click that and make it green :) – Cihan Tek Feb 10 '15 at 23:02
6

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 
      }
   }
Hope
  • 2,096
  • 3
  • 23
  • 40
4

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

Derek Soike
  • 11,238
  • 3
  • 79
  • 74
0

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

func removeTarget(Any?, action: Selector?)