Swift:关闭在当前上下文中显示图片的 ViewController 时不时冻结屏幕
Posted
技术标签:
【中文标题】Swift:关闭在当前上下文中显示图片的 ViewController 时不时冻结屏幕【英文标题】:Swift: Screen Freezing from time to time when Dismissing a ViewController displaying a picture over the current context 【发布时间】:2017-12-12 20:22:54 【问题描述】:我遇到了一些问题:当关闭在当前上下文中显示图片的 ViewController 时,屏幕不时冻结。
有人可以提供一些关于如何解决此问题的见解吗?
我的代码示例如下:
导入 UIKit
类 ViewControllerCell: UICollectionViewCell
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)
【问题讨论】:
调试提示:当屏幕冻结时,在 Xcode 的调试器中暂停应用并检查主线程上发生的情况。 在不使 self 变弱的情况下异步执行诸如关闭之类的 UI 操作会带来很多问题,我建议始终根据情况执行 [unowned self] 的 [weak self](在这种情况下,weak self 就足够了) 然后将 self.dismiss 称为 self?.dismiss。顺便说一句,为什么您需要先分配到全局队列然后再返回主队列?好像没必要 感谢您的帮助!问题已解决! 【参考方案1】:您的异步代码没有意义。您分派到全局队列,然后立即返回主线程。尝试简单地将 dismissPopup() 的实现更改为:
@objc func dismissPopup()
dismiss(animated: true, completion: nil)
【讨论】:
感谢您的帮助!以上是关于Swift:关闭在当前上下文中显示图片的 ViewController 时不时冻结屏幕的主要内容,如果未能解决你的问题,请参考以下文章
如何关闭当前的 ViewController 并在 Swift 中更改为新的 ViewController?
Swift - 从AppDelegate向View Controller添加子视图
关闭一个自定义弹出窗口UIViewController并立即显示另一个自定义弹出窗口UIViewController -SWIFT
Swift Progress View 动画使进度条超过 100%
Google Sign In 调用 App Delegate 中的方法后关闭 View Controller (iOS / Swift)