![](/img/trans.png)
[英]Swift: Screen Freezing from time to time when Dismissing a ViewController displaying a picture over the current context
[英]Screen freezes with no error from time to time when dismissing view in swift
我遇到了一些麻煩:關閉ViewController在當前上下文中顯示圖片時,屏幕有時會凍結。
有人可以為我提供一些有關如何解決此問題的見解嗎?
我嘗試使用暫停按鈕檢查主線程,但未發現任何異常情況。
我的代碼示例如下:
import UIKit
class 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 = SalePopupViewController()
popupViewController.modalPresentationStyle = .overCurrentContext
popupViewController.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
window!.rootViewController?.present(PopupViewController, animated: true, completion: nil)
}
}
SalePopupViewController:
import UIKit
class 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() {
weak var weakself = self
weakself?.dismiss(animated: true, completion: { print("dismissing")})
}
}
}
第一件事是為什么要顯示來自window!.rootViewController?
的彈出window!.rootViewController?
而不是您從中調用它的視圖控制器? 只需使用present(PopupViewController, animated: true, completion: nil)
代替。
第二件事是弱者自己的事情是完全沒有必要的,因為無法將自己從內存中釋放出來,但是您的關閉按鈕仍然存在,因此它可以牢固地引用自己,而弱者則完全沒有區別。
編輯
所以我沒有看到ViewControllerCell是一個集合視圖單元。 現在的解決方案有所不同:您有幾個選擇,1.在為indexPath函數的item編寫單元格時,將viewController引用傳遞給每個單元格
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
if let cell = collectionView.dequeueReusableCell(withIdentifier identifier: String) as? ViewControllerCell {
//do your thing here
cell.parentViewController = self
}}
當然,您必須在collectionViewCell中設置一個公共變量
var parentViewController: UIViewController!
// you can also use the viewController subclass name that you created
//。 如果要使用特定功能。 如果只有禮物那么你就很好
最后,在呈現的地方使用parentViewController.present(....)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.