Swift中第一次加载时序列背景闪烁

Swift中第一次加载时序列背景闪烁,swift,segue,flicker,Swift,Segue,Flicker,在我的第二个视图控制器上第一次加载时,我会在瞬间出现闪烁(可能是视图控制器未完全加载)。我尝试通过一个序列进行预设,并在加载之前在自定义初始值设定项中进行设置。我创建的效果是为每个视图控制器保持相同的背景。请注意,这仅在第一次转换到视图控制器时发生。这不会发生在第一段之后 非常感谢您的帮助。我在下面列出了适用的代码 第一视图控制器: class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSou

在我的第二个视图控制器上第一次加载时,我会在瞬间出现闪烁(可能是视图控制器未完全加载)。我尝试通过一个序列进行预设,并在加载之前在自定义初始值设定项中进行设置。我创建的效果是为每个视图控制器保持相同的背景。请注意,这仅在第一次转换到视图控制器时发生。这不会发生在第一段之后

非常感谢您的帮助。我在下面列出了适用的代码

第一视图控制器:

 class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate, UIScrollViewDelegate
{
// Background Image
var backgroundImage: UIImageView!
var backgroundImages: [String] = ["canyon.jpeg","channel.jpeg","glacier.jpeg","northernlights.jpeg", "jungle"]
var currentBackground = 0

init(_ coder: NSCoder? = nil)
{
    // Logic Goes Here

    if let coder = coder
    {
        super.init(coder: coder)!
    } else
    {
        super.init(nibName: nil, bundle: nil)
    }
}

required convenience init(coder: NSCoder)
{
    self.init(coder)
}

override func viewDidLoad()
{
    setUpView()
    super.viewDidLoad()
}

func setUpView()
{
    backgroundImage = UIImageView(frame: CGRect( x: 0 , y: 0, width: ScreenWidth, height: ScreenHeight))
    backgroundImage.image = UIImage(named: backgroundImages[currentBackground])
    backgroundImage.alpha = 1

    let playButtonPercent:CGFloat = 0.75
    playButton = UIButton(frame: CGRect(x: (ScreenWidth * 0.5) - (ScreenWidth * playButtonPercent / 2), y: (ScreenHeight * (3 / 4)) - (ScreenHeight * (15/480)), width: (ScreenWidth * playButtonPercent), height: ScreenHeight * (30/480)))
    playButton.setTitle("PLAY", for: UIControlState())
    playButton.addTarget(self, action: #selector(self.playRelease), for: .touchUpInside)

    self.view.addSubview(backgroundImage)
    self.view.addSubview(playButton)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if (segue.identifier == "Play")
    {
        if let nextViewController = (segue.destination as? GameListViewController)
        {
            nextViewController.currentBackground = currentBackground
            nextViewController.backgroundImage.image = UIImage(named: self.backgroundImages[currentBackground])
        }
    }
}

func playRelease()
{        
    // Segue
    performSegue(withIdentifier: "Play", sender: self)
}
}
 class GameListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
 {
let backgroundImages = ["canyon.jpeg","channel.jpeg","glacier.jpeg","northernlights.jpeg", "jungle"]
var currentBackground = 0
var backgroundImage: UIImageView!

override func viewDidLoad()
{
    backgroundImage = UIImageView(frame: CGRect( x: 0 , y: 0, width: ScreenWidth, height: ScreenHeight))
    super.viewDidLoad()
    setUpView()
    }
}

func setUpView()
{
    backgroundImage.image = UIImage(named: backgroundImages[currentBackground])
    backgroundImage.alpha = 1

    self.view.addSubview(backgroundImage)
    view.sendSubview(toBack: backgroundImage)
}
}
查看控制器2:

 class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate, UIScrollViewDelegate
{
// Background Image
var backgroundImage: UIImageView!
var backgroundImages: [String] = ["canyon.jpeg","channel.jpeg","glacier.jpeg","northernlights.jpeg", "jungle"]
var currentBackground = 0

init(_ coder: NSCoder? = nil)
{
    // Logic Goes Here

    if let coder = coder
    {
        super.init(coder: coder)!
    } else
    {
        super.init(nibName: nil, bundle: nil)
    }
}

required convenience init(coder: NSCoder)
{
    self.init(coder)
}

override func viewDidLoad()
{
    setUpView()
    super.viewDidLoad()
}

func setUpView()
{
    backgroundImage = UIImageView(frame: CGRect( x: 0 , y: 0, width: ScreenWidth, height: ScreenHeight))
    backgroundImage.image = UIImage(named: backgroundImages[currentBackground])
    backgroundImage.alpha = 1

    let playButtonPercent:CGFloat = 0.75
    playButton = UIButton(frame: CGRect(x: (ScreenWidth * 0.5) - (ScreenWidth * playButtonPercent / 2), y: (ScreenHeight * (3 / 4)) - (ScreenHeight * (15/480)), width: (ScreenWidth * playButtonPercent), height: ScreenHeight * (30/480)))
    playButton.setTitle("PLAY", for: UIControlState())
    playButton.addTarget(self, action: #selector(self.playRelease), for: .touchUpInside)

    self.view.addSubview(backgroundImage)
    self.view.addSubview(playButton)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if (segue.identifier == "Play")
    {
        if let nextViewController = (segue.destination as? GameListViewController)
        {
            nextViewController.currentBackground = currentBackground
            nextViewController.backgroundImage.image = UIImage(named: self.backgroundImages[currentBackground])
        }
    }
}

func playRelease()
{        
    // Segue
    performSegue(withIdentifier: "Play", sender: self)
}
}
 class GameListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
 {
let backgroundImages = ["canyon.jpeg","channel.jpeg","glacier.jpeg","northernlights.jpeg", "jungle"]
var currentBackground = 0
var backgroundImage: UIImageView!

override func viewDidLoad()
{
    backgroundImage = UIImageView(frame: CGRect( x: 0 , y: 0, width: ScreenWidth, height: ScreenHeight))
    super.viewDidLoad()
    setUpView()
    }
}

func setUpView()
{
    backgroundImage.image = UIImage(named: backgroundImages[currentBackground])
    backgroundImage.alpha = 1

    self.view.addSubview(backgroundImage)
    view.sendSubview(toBack: backgroundImage)
}
}

我注意到,第一次加载时,黑色闪烁出现在我的每个视图控制器之间,而不仅仅是第一个。我目前使用push,因为segueI注意到,第一次加载时,黑色闪烁出现在我的每个视图控制器之间,而不仅仅是第一个。我目前使用push作为segue