Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 12th, 2012  |  syntax: None  |  size: 2.73 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. iPhone App: implementation of Drag and drop images in UIView
  2. -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  3.  
  4.         NSLog(@"%f,%f", self.center.x, self.center.y);
  5.         CGPoint newLoc = CGPointZero;
  6.  
  7.         newLoc = [self.mainView convertPoint:[[touches anyObject] locationInView:self.superview] toView:self.superview];
  8.         float newX = newLoc.x + self.superview.frame.origin.x + (self.frame.size.width /2) + [[touches anyObject] locationInView:self].x ;
  9.         float newY = newLoc.y - (((UIScrollView *)self.superview).contentOffset.y *2) ;
  10.         NSLog(@"content offset %f", ((UIScrollView *)self.superview).contentOffset.y);
  11.  
  12.         self.scrollParent.scrollEnabled = NO;
  13.         NSLog(@"%f,%f", self.center.x, self.center.y);
  14.  
  15.         newLoc = CGPointMake(newX, newY);
  16.         [self.superview touchesCancelled:touches withEvent:event];                                                              
  17.         [self removeFromSuperview];
  18.         NSLog(@"%f,%f", self.center.x, self.center.y);
  19.  
  20.  
  21.         self.center = CGPointMake(newLoc.x, newLoc.y);
  22.         [self.mainView addSubview:self];
  23.         NSLog(@"%f,%f", self.center.x, self.center.y);
  24.  
  25.         [self.mainView bringSubviewToFront:self];
  26.         isInScrollview = NO;
  27. }  
  28.  
  29. -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  30.  
  31.     [UIView beginAnimations:@"stalk" context:nil];
  32.     [UIView setAnimationDuration:.001];
  33.     [UIView setAnimationBeginsFromCurrentState:YES];
  34.  
  35.     UITouch *touch = [touches anyObject];
  36.  
  37.     self.center = [touch locationInView: self.superview];
  38.  
  39.     [UIView commitAnimations];
  40.     if ((self.center.x + (self.frame.size.width / 2)) > 150 && hasExitedDrawer && !self.scrollParent.dragging ) {
  41.         self.scrollParent.scrollEnabled = NO;
  42.  
  43.         [self.delegate moveItemsDownFromIndex: ((self.center.y + (self.scrollParent.contentOffset.y)) / 44) + 1 ];
  44.         //NSLog(@"%i", ((self.center.y + (self.scrollParent.contentOffset.y *2)) / 44) + 1);
  45.     }
  46.  
  47.     if (self.center.x + (self.frame.size.width / 2) < 150) {
  48.  
  49.         hasExitedDrawer = YES;
  50.  
  51.     }
  52.  
  53. }  
  54.  
  55. -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  56.  
  57.     if ((self.center.x + (self.frame.size.width / 2)) > 150 && hasExitedDrawer && !self.scrollParent.dragging ) {
  58.         CGPoint newLoc = CGPointZero;
  59.  
  60.         newLoc = [self.scrollParent convertPoint:[[touches anyObject] locationInView:self.superview] toView:self.superview];
  61.         float newY = newLoc.y + (self.scrollParent.contentOffset.y *2);
  62.  
  63.         [self.scrollParent insertSubview:self atIndex:((self.center.y + (self.scrollParent.contentOffset.y)) / 44) ];
  64.         self.frame = CGRectMake(0, newY, self.frame.size.width, self.frame.size.height);
  65.         isInScrollview = YES;
  66.         hasExitedDrawer = NO;
  67.     }
  68. }