Coder Social home page Coder Social logo

bottom-sheet-ios's Introduction

Steps to implement Bottom sheet in your project :

MaryPopin is an extension of UIViewController so you just need to copy past 2 files of objective-c in your project. If you are using an Objective-C project then you just need to use directly. If you are using swift project then you need to create a Bridging header to use.

Create a view controller with the design of popups in the storyboard like below.

Now create two different ViewControllers.swift files to accommodate the coding of these two popups. Also, I have created a base class for the bottom sheet with some common functionality. The common things like pan gesture and some common controls are declared in the base class.

To give same sliding functionality like instagram I have given the Pan gesture like given below.

    @objc func handlePan(_ recognizer: UIPanGestureRecognizer){
        
        let dy = recognizer.translation(in: self.viewMainContainer).y
        let velocity : CGPoint = recognizer.velocity(in: self.viewMainContainer)
        
        /**IF/SWITCH COMMENT :
         What to check : to check the state of gesture
         **/
        switch recognizer.state {
        case .began:
            break
        case .changed:
            if(velocity.y > 0) {
                self.lastDy = Double(dy)
                // when user sliding it down and if it reaches to the some value we will hide it automatically
                if  dy >= CGFloat(self.maxTotalHeight - 240){
                    if !self.isHiddenBottomSheet{
                        self.presentingPopin()?.dismissCurrentPopinController(animated: true, completion: nil)
                    }
                    self.isHiddenBottomSheet = true
                } else {
                    self.viewMainContainerHeightConst.constant = CGFloat(self.maxTotalHeight) - dy
                }
            } else {
                if  Int(dy) >= self.maxTotalHeight{
                    // when user sliding it down and if it reaches to the some value we will hide it automatically else setting it back to its original height
                    self.viewMainContainerHeightConst.constant = CGFloat(self.maxTotalHeight)
                } else {
                    self.viewMainContainerHeightConst.constant = self.viewMainContainerHeightConst.constant + (CGFloat(self.lastDy) - dy)
                }
            }
        case .failed, .ended, .cancelled:
            if !self.isHiddenBottomSheet
            {
                // when user sliding it down and if it reaches to the some value we will hide it automatically
                if  dy >= CGFloat(self.maxTotalHeight - 240){
                    self.presentingPopin()?.dismissCurrentPopinController(animated: true, completion: nil)
                } else {
                    self.viewMainContainerHeightConst.constant = CGFloat(self.maxTotalHeight)
                    UIView.animate(withDuration: 0.5) {
                        self.view.layoutIfNeeded()
                    }
                }
            }
            else{
                
            }
        default:
            break
        }
    }

For detail explanation and understanding please download and check the code. The final result is like,

bottom-sheet-ios's People

Contributors

ravipromact2306 avatar rvagrawal avatar shagunparikh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.