0

I have an image shown on my screen, and I want to be able to select any part of the image and get the (x,y) coordinates of that point and show a Label with a dot where I touched and the coordinates right next to it.

My problem is this. I can touch and change the coordinates text appropriately, but it isn't moving the actual location of the Label on the first touch. It does move to the appropriate place if I touch in the exact same place twice. If I touch somewhere only once though, it will move it back to the original place that it was created.

Here is the code block I use for the touch:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:nil];
    _coordinatesLabel.center = CGPointMake(location.x + 83, location.y - 4);
    _coordinatesLabel.text = [NSString stringWithFormat:@"•  (%.2f,%.2f)", location.x/32, (768-location.y)/32];
}

Edit

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:nil];

    _coordinatesLabel.translatesAutoresizingMaskIntoConstraints = true;

    _coordinatesLabel.center = CGPointMake(location.x + 83, location.y - 4);
    _coordinatesLabel.text = [NSString stringWithFormat:@"•  (%.2f,%.2f)", location.x/32, (768-location.y)/32];

}

I add that middle line of code and it works perfectly. Thanks for the help.

  • 1
    Are you using auto layout on the label? If so, you'll need to consider that the label will be put back into it's originally constrained spot if they're not updated. – Fred Faust May 02 '16 at 14:48
  • Did the image is in scroll view or did you add any gesture to the view? – Surya Subenthiran May 02 '16 at 14:48
  • No scroll view or gesture. It is just a UIView with a UIImageView in it. The label does have Auto Layout enabled, but I don't understand why it would be updated. I'm changing the Label's position. Is there something I need to do to update beyond what is in that block of code? – Russell Thorman May 02 '16 at 14:56
  • Like thefredelement is saying, you need to update the constraints of the label. With auto layout, the layout constraints are what determines the position of controls on the screen. So instead of updating the center, update the constraints. As soon as a layout occurs, the constraints will take priority again and the label will just snap back to its original position. – j.f. May 02 '16 at 15:11

0 Answers0