Note that there are some explanatory texts on larger screens.

plurals
  1. POUIImage detecting touch and dragging
    text
    copied!<p>Fairly common question this, to which I have a few answers and I'm nearly there. I have a button which when pressed, will create an image (code as follows)</p> <p>(numImages is set on load to ZERO and is used as a count up for the tag numbers of all images created)</p> <pre><code>UIImage *tmpImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%i.png", sender.tag]] retain]; UIImageView *myImage = [[UIImageView alloc] initWithImage:tmpImage]; numImages += 1; myImage.userInteractionEnabled = YES; myImage.tag = numImages; myImage.opaque = YES; [self.view addSubview:myImage]; [myImage release]; </code></pre> <p>I then have a touchesBegan method which will detect what's touched. What I need it to do is to allow the user to drag the newly created image. It's nearly working, but the image flickers all over the place when you drag it. I can access the image which you click on as I can get it's TAG, but I just cannot drag it nicely. </p> <pre><code>- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint location = [touch locationInView:touch.view]; if (touch.view.tag &gt; 0) { touch.view.center = location; } NSLog(@"tag=%@", [NSString stringWithFormat:@"%i", touch.view.tag]); } - (void) touchesMoved:(NSSet *)touches withEvent: (UIEvent *)event { [self touchesBegan:touches withEvent:event]; } </code></pre> <p>It works, in that I get an output of the tag for each image as I click on them. But when I drag, it flashes... any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload