简体   繁体   中英

Bottom animation similar to Apple Maps application in iOS

Want to create an animation were user can slide the view from bottom similar to Apple Maps application in iOS. But having issues while handling gesture.

The code in my mainVC in which panVC is added as subview from bottom which works fine.(The panVC correctly comes at bottom)

func displayFromBottom {
   let storyboard = UIStoryboard(name: "Main", bundle: nil)
    listVC = storyboard.instantiateViewController(withIdentifier: “PanViewContr") as! PanViewContr


    var startingFrame = self.view.bounds;
    startingFrame.origin.y = startingFrame.size.height; //Starts from the bottom of the parent.
    startingFrame.size.height = 100; //Has a height of 100.

    var finalFrame = self.view.bounds;
    finalFrame.origin.y = finalFrame.size.height - 100; //100 from the bottom of the parent.

    listVC.view.frame = startingFrame
    listVC.willMove(toParentViewController: self)
    self.addChildViewController(listVC)
    self.view.addSubview(listVC.view)
    listVC.didMove(toParentViewController: self)

    UIView.animate(withDuration: 0.5, animations: {
        self.listVC.view.frame = finalFrame
    }) { complete in
        print("done”)
    }
}

The code for PanVC were pan gesture is handled.

  func slideViewVerticallyTo(_ y: CGFloat) {
    self.view.frame.origin = CGPoint(x: 0, y: y)
}

@IBAction func panGesture(_ panGesture: UIPanGestureRecognizer) {
    switch panGesture.state {
    case .began, .changed:
        // If pan started or is ongoing then
        // slide the view to follow the finger
        let translation = panGesture.translation(in: view)
        let y = max(0, translation.y) //what should be value of y to make it dragable smoothly
        self.slideViewVerticallyTo(y)
        break
    case .ended:
        break
    }

Any hint in right direction is highly appreciated.

I had created a movable extension that adds the ability to move UIView's around a screen the code for which you can see here: https://github.com/szweier/SZUtilities/blob/master/SZUtilities/Classes/UIView/Movable/UIView%2BMovable.swift you may be able to see something in this code that you are missing in your own. I'm sorry my answer isn't more specific but hopefully it gets you moving towards the correct answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM