1

I have 5 images on my UIViewcontroller in Storyboard. 4 of the five images can be moved (graph 1,2,3,4) with the finger on the screen. Then I created an if statement: if I drag one of the images on the fixed one (final graph), on a label appears "text". In my statement I wrote intercept. The only problem is that the text on the label appears as soon as the two images intercept. But I want to make it appear when the user removes the finger to "drop" the image. how can I do that?

Here is the code:

//image movement
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

   UITouch *touch = [[event allTouches] anyObject];

   if ([touch view] == graph1) {
      CGPoint location = [touch locationInView:self.view];
      graph1.center = location;
      [self interceptofgraphs]; 
   }

   if ([touch view] == graph2) {
      CGPoint location = [touch locationInView:self.view];
      graph2.center = location;
     [self interceptofgraphs];
   }

   if ([touch view] == graph3) {
      CGPoint location = [touch locationInView:self.view];
      graph3.center = location;
      [self interceptofgraphs];
   }

   if ([touch view] == graph4) {
      CGPoint location = [touch locationInView:self.view];
      graph4.center = location;
      [self interceptofgraphs];
   }
}

-(void)interceptofgraphs {

   if (CGRectIntersectsRect(graph2.frame, finalgraph.frame))
      graphlabel.text = [NSString stringWithFormat: @"Right answer. Well done!!"];

   if (CGRectIntersectsRect(graph1.frame, finalgraph.frame))
      graphlabel.text = [NSString stringWithFormat: @"Wrong!! not at all."];

   if (CGRectIntersectsRect(graph3.frame, finalgraph.frame))
      graphlabel.text = [NSString stringWithFormat: @"Wrong!! Try again"];

   if (CGRectIntersectsRect(graph4.frame, finalgraph.frame))
       graphlabel.text = [NSString stringWithFormat: @"Wrong!! NO"];
}

Thanks to all of you who answer!!

1
  • Retagged; not an XCode problem. XCode is just the IDE, and this question has nothing to do with that.
    – Almo
    Mar 16, 2012 at 19:07

1 Answer 1

1

This should work:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

  UITouch *touch = [[event allTouches] anyObject];

  //we'll almost certainly need a location of touch
  CGPoint location = [touch locationInView:self.view];

  //we store the reference of a view that was touched into touchedView
  UIView *touchedView = [touch view];

  //if view that was touched is one of the views we're interested in
  if ((touchedView == graph1)||(touchedView == graph2)||
      (touchedView == graph3)||(touchedView == graph4)) {

    //we move it's center
    //since the touchedView hold's a reference to the view that
    //was touched - we're moving the right view for sure
    touchedView.center = location;    
  }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

  //we only want to check whitch view was moved at the end 
  //when user releases the touch
  [self interceptofgraphs];
}
1
  • @Alessandro: added some comments
    – Rok Jarc
    Mar 16, 2012 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.