【问题标题】:Swift Xib UiView BottomSheet being called multiple timesSwift Xib UiView BottomSheet 被多次调用
【发布时间】:2020-07-06 19:40:33
【问题描述】:

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

这里有一些视频让它更清楚

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

我在那个 gdrive 链接上放了一个 gif 图像

代码

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 视图只显示一次,无论您快速单击按钮多少次。

谢谢大家:)

【问题讨论】:

    标签: swift uiview xib


    【解决方案1】:

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

    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-你需要添加一个bool变量,比如

     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
    

    【讨论】:

    • 所以我应该在我的 addBottomSheetView() 中使用你的任何一种方式?
    • 我尝试在 addBottomSheetView func 中添加保护 !isShown 并且我在按下按钮关闭后尝试调用 false ,但仍然多次调用并弹出多次,或者我把它错了吗?
    • 在上述两种情况下,创建一个变量来保存对添加视图的引用,并在您设置 isShown = false 或更好地在答案中使用选项 2 时使用大多数底部代码将其删除
    • 啊,好吧,所以我创建了一个全局变量,不是吗?我使用你的第一个选项,它就像一个魅力,但现在你说选项 2 更好.....所以我应该在大项目中使用选项 1 还是 2?
    • 选项 2 具有 1 个实例变量而不是 2 个的优点
    猜你喜欢
    • 1970-01-01
    • 2016-02-29
    • 2018-07-09
    • 1970-01-01
    • 2018-02-15
    • 2018-06-15
    • 2014-08-13
    • 2016-06-10
    • 1970-01-01
    相关资源
    最近更新 更多