Swift Xib UiView BottomSheet 被多次调用

Swift Xib UiView BottomSheet being called multiple times

所以我用 xib 制作了这个 bottomsheet 视图,我的代码没有任何问题,只是我只想显示一次,我的意思是每次我点击按钮时它都会被触发。这很好,但如果我快速点击按钮,它也会根据我点击的次数加载大量时间。我只想显示一次我的意思是无论你快速点击多少它只会显示一次 xib 视图,直到我关闭 xib 上的按钮并且它会做同样的事情。

这里有一些视频可以更清楚

https://drive.google.com/file/d/12pwGdTiP_1QZlYc8tV-BlIIQQ5yYrfto/view?usp=sharing

我在 gdrive 上放了一个 gif link

求代码

Xib 控制器:

OrderActionSheetView: UIViewController {

@IBOutlet weak var Text: UILabel!

@IBOutlet weak var vieww: UIView!

@IBOutlet weak var botView: UIView!
@IBAction func cobaLagiBTn(_ sender: Any) {
    
    let closeView = screenSize.height
    UIView.animate(withDuration: 0.3, animations: {
        self.view.alpha = 0.0
        self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)

        let frame = self.view.frame
        self.view.frame = CGRect(x: 0, y: closeView, width: frame.width, height: frame.height)
        
    })
}


let fullView: CGFloat = 0
let screenSize: CGRect = UIScreen.main.bounds

override func viewDidLoad() {
    super.viewDidLoad()
    

    
    vieww.layer.cornerRadius = 20
    vieww.layer.borderWidth = 0.5
    vieww.layer.borderColor = UIColor(red:222/255, green:225/255, blue:227/255, alpha: 1).cgColor
    vieww.clipsToBounds = true
    
    botView.layer.masksToBounds = false
    botView.layer.shadowColor = UIColor.black.cgColor
    botView.layer.shadowOpacity = 0.14
    botView.layer.shadowOffset = CGSize(width: 0, height: 0)
    botView.layer.shadowRadius = 2.7
    
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    

    UIView.animate(withDuration: 0.3) { [weak self] in
        let frame = self?.view.frame

        let yComponent = self?.fullView
        self?.view.frame = CGRect(x: 0, y: yComponent!, width: frame!.width, height: frame!.height)
    }
}

func prepareBackgroundView(){
    let blurEffect = UIBlurEffect.init(style: .light)
    let visualEffect = UIVisualEffectView.init(effect: blurEffect)
    let bluredView = UIVisualEffectView.init(effect: blurEffect)
    bluredView.contentView.addSubview(visualEffect)

    visualEffect.frame = UIScreen.main.bounds
    bluredView.frame = UIScreen.main.bounds

    view.insertSubview(bluredView, at: 0)
}



func Show(){
    
    self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
    self.view.alpha = 0.0
    UIView.animate(withDuration: 0.25, animations: {
        self.view.alpha = 1.0
        self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
    })
    }
 }

视图控制器:

class BottomSheetViewController: UIViewController {

@IBAction func Button(_ sender: Any) {
    
    let text = "Connection Failed"
     addBottomSheetView(text: text)
    
}
override func viewDidLoad() {
    super.viewDidLoad()
    
}



func addBottomSheetView(text : String) {
    // 1- Init bottomSheetVC
    let bottomSheetVC = OrderActionSheetView()
    
    // 2- Add bottomSheetVC as a child view
    self.addChild(bottomSheetVC)
    self.view.addSubview(bottomSheetVC.view)
    bottomSheetVC.didMove(toParent: self)
    
    // 3- Adjust bottomSheet frame and initial position.
    let height = view.frame.height
    let width  = view.frame.width
    bottomSheetVC.view.frame = CGRect(x: 0, y: self.view.frame.maxY, width: width, height: height)
    
    bottomSheetVC.Text.text = text
    }

 }

我只需要知道如何停止弹出两次,因为它确实有点大错误.....

正如我之前所说,无论您快速单击按钮多少次,我只希望 xib 视图只显示一次。

谢谢大家:)

在这里,每次点击都会添加一个新实例

func addBottomSheetView(text : String) {
   // 1- Init bottomSheetVC
   let bottomSheetVC = OrderActionSheetView()

   // 2- Add bottomSheetVC as a child view
   self.addChild(bottomSheetVC)
   self.view.addSubview(bottomSheetVC.view)
    bottomSheetVC.didMove(toParent: self)

所以要么

1-您需要添加一个布尔变量,如

 var isShown = false

并在方法的开头添加此代码

guard !isShown else { return }
isShown = true

并且当您删除视图时

isShown = false

2-创建一个实例变量,如

var bottomSheetVC:OrderActionSheetView?

并且在方法的开头

guard bottomSheetVC == nil else { return }
bottomSheetVC = OrderActionSheetView()

当你删除它时

bottomSheetVC.view.removeFromSuperview()
bottomSheetVC = nil