Ios Swift:当关闭在当前上下文中显示图片的ViewController时,屏幕会不时冻结
我遇到了一些问题:当关闭ViewController在当前上下文中显示图片时,屏幕有时会冻结 有人能给我一些关于如何解决这个问题的见解吗 我的代码示例如下: 导入UIKit 类ViewControllerCell:UICollectionViewCell{Ios Swift:当关闭在当前上下文中显示图片的ViewController时,屏幕会不时冻结,ios,swift,uiviewcontroller,uicollectionviewcell,photo,Ios,Swift,Uiviewcontroller,Uicollectionviewcell,Photo,我遇到了一些问题:当关闭ViewController在当前上下文中显示图片时,屏幕有时会冻结 有人能给我一些关于如何解决这个问题的见解吗 我的代码示例如下: 导入UIKit 类ViewControllerCell:UICollectionViewCell{ override init(frame: CGRect) { super.init(frame: frame) backgroundColor = UIColor.white addSubview(showPhot
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.white
addSubview(showPhotoButton)
showPhotoButton.leftAnchor.constraint(equalTo: leftAnchor, constant: 200).isActive = true
showPhotoButton.bottomAnchor.constraint(equalTo: topAnchor, constant: 160).isActive = true
showPhotoButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
showPhotoButton.widthAnchor.constraint(equalToConstant: 70).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
lazy var showPhotoButton: UIButton = {
let button = UIButton(type: .system)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Show", for: .normal)
button.addTarget(self, action: #selector(showSale), for: .touchUpInside)
button.setTitleColor(UIColor(r: 120, g: 80, b: 255), for: .normal)
return button
}()
@objc func showSale() {
let popupViewController = PopupViewController()
popupViewController.modalPresentationStyle = .overCurrentContext
popupViewController.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
window!.rootViewController?.present(PopupViewController, animated: true, completion: nil)
}
}
导入UIKit
类SalePopupViewController:UIViewController{
override func viewDidLoad() {
view.backgroundColor = UIColor(white: 1, alpha: 0.60)
view.isOpaque = false
view.addSubview(rebateImage)
view.addSubview(dismissButton)
dismissButton.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
dismissButton.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
dismissButton.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
dismissButton.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
rebateImage.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
rebateImage.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -35).isActive = true
rebateImage.heightAnchor.constraint(equalToConstant: 290).isActive = true
rebateImage.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1).isActive = true
}
let dismissButton: UIButton = {
let button = UIButton(type: .system)
button.addTarget(self, action: #selector(dismissPopup), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
let rebateImage: UIImageView = {
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
image.layer.masksToBounds = false
image.layer.cornerRadius = 2
image.contentMode = .scaleAspectFill
image.clipsToBounds = true
image.image = UIImage(named: "SaleCostco")
return image
}()
@objc func dismissPopup() {
DispatchQueue.global(qos: .userInitiated).async {
DispatchQueue.main.async {
self.dismiss(animated: true, completion: nil)
}
}
}
}您的异步代码没有意义。您将调度到一个全局队列,然后立即返回到主线程。尝试将dismissPopup()的实现更改为:
调试提示:当屏幕冻结时,将应用程序暂停在Xcode的调试器中,并检查主线程上发生的情况。在不使self-weak的情况下异步执行disclose等UI操作会带来许多问题,我建议根据情况始终执行[unowned self]的[weak self](在这种情况下,弱self就足够了)然后将self.disclose称为self?.disclose。顺便问一下,为什么您需要先调度到全局队列,然后再返回到主队列?似乎没必要谢谢你的帮助!问题解决了!谢谢你的帮助!
@objc func dismissPopup() {
dismiss(animated: true, completion: nil)
}