在Swift中用手滑动顺利解雇

问题描述 投票:-1回答:2

嗨我正在使用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
            }
        }
    }
ios swift swipe dismiss
2个回答
0
投票

只需从案例UISwipeGestureRecognizer.Direction.down中删除该行:self.dismiss(animated:true,completion: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
        }
    }
}

0
投票

请在您的func中使用此代码,并根据您的要求更改持续时间

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
    })
}
© www.soinside.com 2019 - 2024. All rights reserved.