title: LongPress on UIIImageView, create a clone "ghost" image, then drag it around
body: <p>I have a sample project where I am trying to test out this behavior.  I have two UIImageViews side by side.  I want to be able to longPress on either the left or right UIImageView, create a semi-transparent clone image, and drag it to the other UIImageView to swap images.</p>
  
  <p>For example, to swap the images, the user could do:</p>
  
  <ol>
  <li>Tap and hold on the left UIImageView</li>
  <li>A cloned, smaller "ghost" image will appear at the touch coordinate</li>
  <li>The user will drag the cloned image to the right UIImageView</li>
  <li>The user will release their finger from the screen to "drop" the cloned image</li>
  <li>The left and right UIImageViews can then swamp their images.</li>
  </ol>
  
  <p>Here are some pics to illustrate:</p>
  
  <p>Original state: </p>
  
  <p><a href="http://d.pr/i/PNVc" rel="nofollow">http://d.pr/i/PNVc</a></p>
  
  <p>After long press on left-side UIImageView with smaller cloned image added as subview:</p>
  
  <p><a href="http://d.pr/i/jwxj" rel="nofollow">http://d.pr/i/jwxj</a></p>
  
  <p>I can detect the longpress and make the cloned image, but I cannot pan that image around unless I release my finger and do another touch on the screen.</p>
  
  <p>I'd like to be able to be able to do it all in one motion, without the user needing to remove their finger from the screen.  </p>
  
  <p>I don't know if this is the right approach, but this is how I'm doing it for now.  Thanks for any help!</p>
  
  <pre><code>- (void)viewDidLoad
  {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
  
      [self addLongPressGestureToPiece:leftImageView];
      [self addLongPressGestureToPiece:rightImageView];
  }
  
  - (void)addLongPressGestureToPiece:(UIView *)piece
  {
      NSLog(@"addLongPressGestureToPiece");
      UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressPiece:)];
      [longPressGesture setDelegate:self];
      [piece addGestureRecognizer:longPressGesture];
      [longPressGesture release];
  }
  
  - (void)addPanGestureRecognizerToPiece:(UIView *)piece
  {
      NSLog(@"addPanGestureRecognizerToPiece");
      UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
      [panGesture setMaximumNumberOfTouches:1];
      [panGesture setDelegate:self];
      [piece addGestureRecognizer:panGesture];
      [panGesture release];
  }
  
  - (void)longPressPiece:(UILongPressGestureRecognizer *)gestureRecognizer
  {
      UIImageView *piece = (UIImageView*)[gestureRecognizer view];
  
      CGPoint point = [gestureRecognizer locationInView:self.view];
  
      if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
      {
          NSLog(@"UIGestureRecognizerStateBegan");
  
          // create the semi-transparent imageview with the selected pic
  
          UIImage *longPressImage = [piece image];
          UIImageView *draggableImageView = [[UIImageView alloc] initWithFrame:CGRectMake(point.x - longPressImage.size.width/6/2, point.y - longPressImage.size.height/6/2, longPressImage.size.width/6, longPressImage.size.height/6)];
          draggableImageView.image = longPressImage;
          draggableImageView.alpha = 0.5;
          draggableImageView.userInteractionEnabled = YES;
          [self.view addSubview:draggableImageView];
  
          [self addPanGestureRecognizerToPiece:draggableImageView];
          photoView.userInteractionEnabled = NO;
      }
      else if(gestureRecognizer.state == UIGestureRecognizerStateChanged)
      {
          NSLog(@"Changed");
      }
      else if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
      {
          NSLog(@"Ended");
          photoView.userInteractionEnabled = YES;
      }
  
  }
  
  
  - (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
  {
      NSLog(@"adjustAnchorPointForGestureRecognizer");
      if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
          UIView *piece = gestureRecognizer.view;
          CGPoint locationInView = [gestureRecognizer locationInView:piece];
          CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];
          piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
          piece.center = locationInSuperview;
      }
  }
  
  - (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
  {
      NSLog(@"pan piece");
      UIView *piece =[gestureRecognizer view];
  
      [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
  
      CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
  
      // if velocity.y is positive, user is moving down, if negative, then moving up
      CGPoint velocity = [gestureRecognizer velocityInView:[piece superview]];
  
      if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged)
      {
          [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
  
          [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
      }
      else if([gestureRecognizer state] == UIGestureRecognizerStateEnded)
      {
          NSLog(@"piece y %f", piece.frame.origin.y);
  
      }
  }
  </code></pre>