I am building an app that will have objects inside of a scrollView at the top of the screen, and want users to be able to drag these subviews into a new scrollView below.
Hierarchy looks like this:
- Superview
- Top UIScrollView
- DraggableView class objects
- Middle UIScrollView (droppable)
- Bottom UIScrollView (droppable)
- Top UIScrollView
I have already gotten the subviews to be draggable, but they only stay visible within their superview (the top scroll view). I want to be able to drag them anywhere in the main superview (i.e. controller's self.view) I tried passing the main superview to the draggable view by setting the subview's property, 'dragView'.
Here is the dragging method I added to the class, "DraggableView", which is a subclass of UIView.
Code:
class DraggableView: UIView
{
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
var dragView: UIView! // Superview to pass in
override func touchesMoved(touches: NSSet, withEvent event: UIEvent)
{
let touch = touches.anyObject() as UITouch
let location = touch.locationInView(self.dragView)
let oldLocation = touch.previousLocationInView(self.dragView)
self.frame = CGRectOffset(self.frame, location.x-oldLocation.x, location.y-oldLocation.y)
}
}