- iPhone App: implementation of Drag and drop images in UIView
- -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- NSLog(@"%f,%f", self.center.x, self.center.y);
- CGPoint newLoc = CGPointZero;
- newLoc = [self.mainView convertPoint:[[touches anyObject] locationInView:self.superview] toView:self.superview];
- float newX = newLoc.x + self.superview.frame.origin.x + (self.frame.size.width /2) + [[touches anyObject] locationInView:self].x ;
- float newY = newLoc.y - (((UIScrollView *)self.superview).contentOffset.y *2) ;
- NSLog(@"content offset %f", ((UIScrollView *)self.superview).contentOffset.y);
- self.scrollParent.scrollEnabled = NO;
- NSLog(@"%f,%f", self.center.x, self.center.y);
- newLoc = CGPointMake(newX, newY);
- [self.superview touchesCancelled:touches withEvent:event];
- [self removeFromSuperview];
- NSLog(@"%f,%f", self.center.x, self.center.y);
- self.center = CGPointMake(newLoc.x, newLoc.y);
- [self.mainView addSubview:self];
- NSLog(@"%f,%f", self.center.x, self.center.y);
- [self.mainView bringSubviewToFront:self];
- isInScrollview = NO;
- }
- -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
- [UIView beginAnimations:@"stalk" context:nil];
- [UIView setAnimationDuration:.001];
- [UIView setAnimationBeginsFromCurrentState:YES];
- UITouch *touch = [touches anyObject];
- self.center = [touch locationInView: self.superview];
- [UIView commitAnimations];
- if ((self.center.x + (self.frame.size.width / 2)) > 150 && hasExitedDrawer && !self.scrollParent.dragging ) {
- self.scrollParent.scrollEnabled = NO;
- [self.delegate moveItemsDownFromIndex: ((self.center.y + (self.scrollParent.contentOffset.y)) / 44) + 1 ];
- //NSLog(@"%i", ((self.center.y + (self.scrollParent.contentOffset.y *2)) / 44) + 1);
- }
- if (self.center.x + (self.frame.size.width / 2) < 150) {
- hasExitedDrawer = YES;
- }
- }
- -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
- if ((self.center.x + (self.frame.size.width / 2)) > 150 && hasExitedDrawer && !self.scrollParent.dragging ) {
- CGPoint newLoc = CGPointZero;
- newLoc = [self.scrollParent convertPoint:[[touches anyObject] locationInView:self.superview] toView:self.superview];
- float newY = newLoc.y + (self.scrollParent.contentOffset.y *2);
- [self.scrollParent insertSubview:self atIndex:((self.center.y + (self.scrollParent.contentOffset.y)) / 44) ];
- self.frame = CGRectMake(0, newY, self.frame.size.width, self.frame.size.height);
- isInScrollview = YES;
- hasExitedDrawer = NO;
- }
- }