2017-12-12 17 views
0

私はいくつかの問題を抱えています:時々画面がフリーズするViewControllerを解除して現在のコンテキストで画像を表示しています。Swift:時々画面がフリーズする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) 
     } 
    } 


} 

}

+0

:これに単に)dismissPopup(の実装を変更しようとすると、画面がフリーズし、Xcodeのデバッガの中でアプリを一時停止し、あなたのメインスレッド上で何が起こっているかを調べます。 –

+0

自己弱体化をせずに非同期に消えるようなUI操作は、多くの問題を抱えていますので、状況に応じて(弱い自己は適切でしょう)、自己を参照することをお勧めします。自己として却下する。ところで、なぜグローバルキューにdipしてメインキューに戻す必要がありますか?必要でないようです。 –

+0

ありがとうございました!問題が修正されました。 –

答えて

1

あなたの非同期コードは意味がありません。グローバルキューにディスパッチした後、すぐにメインスレッドに戻ってきます。先端のデバッグ

@objc func dismissPopup() { 
    dismiss(animated: true, completion: nil) 
} 
+0

あなたの助けてくれてありがとう! –

関連する問題