Expand UIView with pan gesture in iOS Swift

2.6k Views Asked by At

I want to implement a view that stays at the bottom of the screen and can be expanded with a pan gesture just like in the Uber app for iOS? Uber Home screen The view will be minimizable when dragged downwards

1

There are 1 best solutions below

1
Sivajee Battina On

There are few third party libraries which makes your work easy. This is one of it. LNPopUpController.

Or else, if you want to customise the code and write: Take one view controller and add a UIView on top of it. Now add Pan Gesture to view like below.

func handlePan(pan : UIPanGestureRecognizer) { 

   let velocity = pan.velocityInView(self.superview).y //; print("Velocity : \(velocity)")

    var location = pan.locationInView(self.superview!) //; print("Location : \(location)")

    var movement = self.frame
    movement.origin.x = 0
    movement.origin.y = movement.origin.y + (velocity * 0.05) //; print("Frame.y : \(movement.origin.y)")

    if pan.state == .Ended{
        print("Gesture Ended")
        panGestureEnded()
    }else if pan.state == .Began { print("Gesture Began")
        let center = self.center
        offset.y = location.y - center.y //;  print("Offset.y : \(offset.y)")


    }else{ print("Gesture else")
        animator.removeBehavior(snap)

        // Apply the initial offset.
        location.x -= offset.x
        location.y -= offset.y

        //print("location.y : \(location.y)")

        // Bound the item position inside the reference view.
        location.x = self.superview!.frame.width / 2

        // Apply the resulting item center.
        snap = UISnapBehavior(item: self, snapToPoint: location)
        animator.addBehavior(snap)


  }


}