2

I'm trying to create functionality that will allow users to create a new UIView by touching a TableViewCell and then dragging the newly created UIView to another part of the screen where they can drop it. The problem is, I can accomplish this in two gestures, but for the life of me, I can't figure out how to do it in one gesture.

I've done some extensive reading and I found some promising answers, but none of them seem to work for me.

Tutorial on How to drag and drop item from UITableView to UITableView

Manually passing touches Began to a newly created instance

The specific issue that I keep running into, is that the create and drag functionality seems to work while the finger is over the TableViewCell, but as soon as it leaves the cell, the dragging stops and the user is forced to grab the item again, at which point everything works fine.

Each TableViewCell is overriding the touchesBegan function with the following code ('delegate' is the main view controller):

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    // Store the touch location
    let touch = touches.anyObject()! as UITouch
    delegate?.touchLocation = touch.locationInView(self.superview!.superview!.superview!)
    // Add a new draggable UIView to the main view
    delegate?.addNewDraggableItem(self)
}

The main view controller has a custom UIView subclass that overrides the hitTest function:

override func hitTest (point: CGPoint, withEvent event: UIEvent?) -> UIView {
    var rv: UIView? = nil

    for child in self.subviews as [UIView] {
        if(child.isKindOfClass(DraggableView)) {
            rv = child;
            child.center = point;
            break;
        }
    }

    if( rv == nil ){
        rv = super.hitTest(point, withEvent:event)
    }
    return rv!
}

An ideas as to what am I doing wrong?

Community
  • 1
  • 1
Ethan Strider
  • 7,849
  • 3
  • 24
  • 29

1 Answers1

0

I have implemented drag&drop by UILongPressGestureRecognizer.
How about this?

view hierarchy:

view (with UILongPressGestureRecognizer)
 |- tableView1 (source) 
 |- tableView2 (destination)
  1. check long-pressed point whether inside tableView1's cell or not(tableView.indexPathForRowAtPoint).
  2. if identify the cell, add dragging-item-view to view and start dragging.
  3. when point inside tableView2 and gesture state is .Ended, remove item-view and end dragging.

Anyway, gesture's state is the key.

bluedome
  • 2,449
  • 1
  • 22
  • 17