在Swift中手动滑动即可轻松解雇

时间:2019-04-18 12:05:44

标签: ios swift swipe dismiss

嗨,我正在使用SwipeGesture在用户向下滑动视图时关闭视图。 我想在顺滑向下滑动时消除视图。 我为用户向下滑动编写了以下代码;它会自动运行,但我不想当时取消视图。

 @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
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

只需从case UISwipeGestureRecognizer.Direction.down中删除该行:      self.dismiss(动画:true,完成:nil),最后将其写入您的函数中。

 @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
        }
    }
}

答案 1 :(得分:0)

请在函数内部使用此代码,并根据您的要求更改持续时间

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
    })
}