1

I am working on a iPhone app, where I need to drag an image from UITableViewCell
and drop it in some other UIView outside the tableview.

(imageview added as :[cell.contentView addSubview:imageView]).

Please help me on this, any useful hint , sample code ?? Touch event delegates are not triggering since i am adding UIImageView to UITableViewCell.

CRDave
  • 9,279
  • 5
  • 41
  • 59
Samir Jwarchan
  • 1,027
  • 11
  • 14

1 Answers1

1

You will not get much event in UIImageView so use UIButton and try something like this to drag:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(imageTouch:withEvent:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button setImage:[UIImage imageNamed:@"vehicle.png"] forState:UIControlStateNormal];
[self.view addSubview:button];

- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
    CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
    UIControl *control = sender;
    control.center = point;
}

when your center point match to other UIView use [UIView adSubview:] method to add UIButton in UIView

I have take this code from Basic Drag and Drop in iOS

Have a look at : Drag an Image within the Bounds of Superview

Community
  • 1
  • 1
CRDave
  • 9,279
  • 5
  • 41
  • 59
  • to implement it with UIImageView this is a simple demo : http://mobiledevelopertips.com/graphics/drag-an-image-within-the-bounds-of-superview.html – CRDave Jan 11 '13 at 05:54
  • Yes, it does helps to move the button but since button is inside UITableViewCell, so when dragging button outside tableview , button will be behind the other views/main view. The problem is we can not see the dragging movement of button outside the tableview (its hidden behind otherviews). – Samir Jwarchan Jan 11 '13 at 06:18
  • You have to change parent view every time UIButton cross bounder of its curent parent. Or you can do is when UIButton start drag move that to main parent UIView (self.view) and bring it to front. – CRDave Jan 11 '13 at 06:54
  • I have Done [[button superview] bringSubviewToFront: button]; that brings the button to top of UItableView , but can you provide me a line of code for bringing that button to the top of main view(window) because [self.view bringSubviewToFront:button]; is not working in mycase, button is still behind the main view. – Samir Jwarchan Jan 11 '13 at 07:03
  • If we going to add as [self.view addsubview:imgbtn] , then the button will appear somewhere else in the view , then in its original position. For eg: button in cell has a origin say (0,0) , if we add that button to self.view then that button will appear on the top left of the screen!! – Samir Jwarchan Jan 11 '13 at 07:15
  • ye u have to change frame according to new view position. there are 4 method to do this : – convertPoint:toView: – convertPoint:fromView: – convertRect:toView: – convertRect:fromView: – CRDave Jan 11 '13 at 07:21
  • Go here and learn about this 4 method http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instm/UIView/convertPoint:toView: – CRDave Jan 11 '13 at 07:22