1

I have an outlet called myImage for an imageView that is set up with an animation of 5 images. With a Tap Gesture Recognizer on it, I want to have the users tap on the correct image as it quickly passes to move onto the next level.

There is an error in my code and this does not work:

- (void)viewDidLoad {
  [super viewDidLoad];

  self.myImage.animationImages =
    [NSArray arrayWithObjects:
      [UIImage imageNamed:@"examplePic1.png"],
      [UIImage imageNamed:@"examplePic2.png"],
      [UIImage imageNamed:@"correctPic.png"],
      [UIImage imageNamed:@"examplePic4.png"],                                 
      [UIImage imageNamed:@"examplePic5.png"],
      nil];

  [self.myImage setAnimationRepeatCount:0];
  self.myImage.animationDuration = 1;
  [self.myImage startAnimating];
}

- (IBAction)TapGestureRecognizer:(id)sender {
  if (self.myImage.image = [UIImage imageNamed:@"correctPic.png"]) {
    // Launch next level
  }
  else {
    // Vibrate Device and subtract points from score
  }
}
MattP
  • 1,920
  • 1
  • 15
  • 22
Peter Schultz
  • 219
  • 1
  • 11
  • Did you mean to assign the image (using `=`) in `TapGestureRecognizer` or were you trying to compare it (using `==`)? – MattP May 27 '15 at 01:37
  • Compare I guess. @MattP, my goal was that when the user taps on the TapGestureRecognizer, if the image is the correct one when they tap it, it launches the next level. Whereas if the user taps on it and 'self.myimage' is not '@"corrrectPic.png" they lose points. – Peter Schultz May 27 '15 at 04:02
  • You are comparing 2 images of type "id". Hence instead of comparison operator "==" use `isEqual`. – byJeevan May 27 '15 at 04:19
  • @jeekOnline When I type this code there is no error but this code does not work. 'if ([self.myImage.image isEqual:[UIImage imageNamed:@"NM17C"]]) {' – Peter Schultz May 27 '15 at 13:24

2 Answers2

1

You have a fundamental misconception in your code about objects and equality. I assume that you meant "==" in your TapGestureRecognizer method. However, even if you changed the = to == would not give you what you want.

The expressions like

[UIImage imageNamed:@"examplePic1.png"]

call a class method of UIImage that creates a new UIImage object. You have created 5 of these objects, put them into an array, an set the animationImage array of your UIImage object to these images. These objects are all different objects from myView.image: I can tell because I can see from your code that you have assigned 5 new objects to the animationObjects array. Your == test cannot ever work because you are comparing self.image to a brand new UIImage object.

What you are trying to do is determine which image the user touched. There is no method in UIImageView that tells you which animation UIImage is currently showing. You are assuming that the image property of the view will be changed as the animation images are displayed, but there is no reason for this assumption to be true: it is certainly not stated in the documentation. Even if you corrected your code (by doing something like this):

if(self.image == [self.animationImages objectAtIndex:2]){

}

your code would not be correct.

If I were trying to do what you want, I would create a UIView with 5 UIImageView as subview, each with their own gesture recognizer. I would use the frame property of each subview to do my animation.

Nischal Hada
  • 3,230
  • 3
  • 27
  • 57
  • Do you mean that I should have 5 UIImageViews and animate each but lets say the fourth one in the sequence is the correct one, so I put the Tap Gesture Recognizer on the fourth ImageView and none on the rest? – Peter Schultz May 27 '15 at 04:08
  • You could do that or you can have the same Tap Gesture Recognizer in each UIImageView, with some logic in the recognizer code to decide if it is the right image. –  May 27 '15 at 10:48
0

Looks like the options are to determine which image "should" be showing based upon the startTime of the animation.

Determine which of its animationImages a UIImageView is displaying?

Or, just use a timer to set the currently displayed image.

Detect which image was clicked in UIImageView with animationImages

Community
  • 1
  • 1
picciano
  • 22,341
  • 9
  • 69
  • 82