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?