Search code examples
iosuiviewuiviewcontrolleruisplitviewcontrolleruisplitview

What IOS approach/control for dragging the point two UIViews meet? (example mockup included)


Question

What IOS approach is recommended (or is there an IOS control for this I'm not aware of) to allowing the user to drag the spit between two UIView up and down?

Say there is View A at the top of the screen and View B at the bottom (within a container view), then the ability to allow the user to drag the horizon split point up or down. (so I'm not talking about the iPad/IOS split view controller here just to be clear, or the new application split views in IOS9)

Aspects of the Question of Interest

  1. Is there an existing IOS control that can do this?
  2. Is there a well know library people use for this?
  3. Is the concept (if doing it yourself) to perhaps do this:
    • setup autolayout constraints for all the views within the container view (see below)
    • set grabHandle constraint to be a given distance from top
    • set top view to have it's bottom dependant on where grabHandle is, and same for the top of the bottom view
    • add gestureHandler to the grabHandler then when it moves somehow pass the location back to the UI Container view where...
    • it adjusts the new position by changing the "grabHandleDistanceFromTheTopConstraint"?

Is this the way to try to implement this now with autolayout and all?

enter image description here


Solution

  • Actually I think this may have it (seems to work ok) - any advice/comments?

    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var dragHandle: UIView!
        @IBOutlet weak var dragHandleTopConstaint: NSLayoutConstraint!
    
        var lastLocation:CGPoint = CGPointMake(0, 0)
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Allow dragging
            let pan = UIPanGestureRecognizer(target:self, action:"pan:")
            pan.maximumNumberOfTouches = 1
            pan.minimumNumberOfTouches = 1
            self.dragHandle.addGestureRecognizer(pan)
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
    
        func pan(rec:UIPanGestureRecognizer) {
            switch rec.state {
            case .Began:
                self.lastLocation = self.dragHandle.center
            case .Changed:
                let translation  = rec.translationInView(self.dragHandle)
                let newLocation : CGPoint = CGPointMake(lastLocation.x, lastLocation.y + translation.y)
    
                self.dragHandleTopConstaint.constant = newLocation.y
            default:
                ()
            }
        }
    
    }
    

    enter image description here