-1

Hi I am using SwipeGesture for dismissing the view when the user swipes down on the view. I want to dismiss the view at swipe down with smooth. I write the following code for when the user swipes down; it works automatically, but I don't want to dismiss the view at the time.

 @objc func respondToSwipeGesture(gesture: UIGestureRecognizer){
        func closeOptionsPanel(){

            DispatchQueue.main.async(execute: {
                let animation = CATransition()
                animation.type = kCATransitionReveal
                animation.subtype = kCATransitionFromLeft
                animation.duration = 1.0
                animation.delegate = self as? CAAnimationDelegate
                animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            })
//            UIView.animate(withDuration:10000000) {
//                self.view.layoutIfNeeded()
//            }
        }

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {


            switch swipeGesture.direction {
            case UISwipeGestureRecognizer.Direction.right:
                print("Swiped right")
            case UISwipeGestureRecognizer.Direction.down:

                closeOptionsPanel()
                self.dismiss(animated: true, completion: nil)

            case UISwipeGestureRecognizer.Direction.left:
                print("Swiped left")
            case UISwipeGestureRecognizer.Direction.up:
                print("Swiped up")
            default:
                break
            }
        }
    }
srikanth kumar
  • 77
  • 1
  • 10
  • 1
    then when you want to dismiss ? you question is incomplete or you are unable to elaborate your problem please make your question more clear. – Abu Ul Hassan Apr 18 '19 at 12:55
  • If you want the gesture itself to _drive_ the dismissal of the view, in real time, you need to write a _custom interactive transition animation_. – matt Apr 18 '19 at 15:57
  • I want to dismiss the view in swipe down with smoothly – srikanth kumar Apr 19 '19 at 04:35

2 Answers2

1

Just remove that line from case UISwipeGestureRecognizer.Direction.down: self.dismiss(animated: true, completion: nil) and write it on your function at the end.

 @objc func respondToSwipeGesture(gesture: UIGestureRecognizer){
     func closeOptionsPanel(){

        DispatchQueue.main.async(execute: {
           let transition: CATransition = CATransition()
    let transition: CATransition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    transition.type = CATransitionType.reveal
    transition.subtype = CATransitionSubtype.fromRight
    self.view.window!.layer.add(transition, forKey: nil)
    self.dismiss(animated: false, completion: nil)
   })
    }

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {


        switch swipeGesture.direction {
        case UISwipeGestureRecognizer.Direction.right:
            print("Swiped right")
        case UISwipeGestureRecognizer.Direction.down:

            closeOptionsPanel()
        case UISwipeGestureRecognizer.Direction.left:
            print("Swiped left")
        case UISwipeGestureRecognizer.Direction.up:
            print("Swiped up")
        default:
            break
        }
    }
}
0

Please use this code inside your func and vary time duration according to your Requirement

func closeOptionsPanel() {
    self.view1.alpha = 1
    self.view1.isHidden = false
    UIView.animate(withDuration: 1.0, animations: {
        self.view1.alpha = 0
    }, completion: {
        finished in
        self.view1.isHidden = true
    })
}
regina_fallangi
  • 2,080
  • 2
  • 18
  • 38