2
votes

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)

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)
     }
}
1
Do you have a solution for that? - JULIIncognito

1 Answers

1
votes

I don't think you can drag it out of superviews bounds. What you can do is to:

  1. create a copy of your draggable view
  2. add it to Superview
  3. delete the draggable view from Top UIScrollView
  4. drag it to Midlle UIScrollView
  5. drop it on top of it
  6. delete it from Superview
  7. and create it again and add it to Middle UIScrollView