尝试在 UIImageView 中的图像之间淡入淡出时出现白屏
White screen when attempting to fade between images in UIImageView
我试图通过使用 UIImageView 并通过设置过渡动画来在两个图像之间平滑过渡,但似乎过渡没有按预期进行,因为出现了白屏而不是新图像淡入。
我用了作为参考,不知道哪里出错了:(
class ViewController: UIViewController {
var bckgImageView = UIImageView(image: UIImage(named: "one"))
override func viewDidLoad() {
super.viewDidLoad()
bckgImageView.frame = self.view.frame
self.view.addSubview(bckgImageView)
animateBackground()
}
func animateBackground() {
UIView.transition(with: self.view,
duration: 5,
options: .transitionCrossDissolve,
animations: { self.bckgImageView.image = UIImage(named: "two") },
completion: nil)
}
在 viewDidLoad 中尚未计算 AutoLayout
您可以在viewDidLayoutSubviews 中更改布局时设置框架。
试试这个
class ViewController: UIViewController {
var bckgImageView = UIImageView(image: UIImage(named: "one"))
override func viewDidLoad() {
super.viewDidLoad()
bckgImageView.frame = self.view.frame
self.view.addSubview(bckgImageView)
animateBackground()
}
func animateBackground() {
UIView.transition(with: self.view,
duration: 5,
options: .transitionCrossDissolve,
animations: { self.bckgImageView.image = UIImage(named: "two") },
completion: nil)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
bckgImageView.frame = self.view.frame
}
}
首先,您要将图像视图的框架设置为 "root" 视图的框架,即在 viewDidLoad()
中定义的 not-yet。你最好使用约束并让 auto-layout 调整视图的大小。
其次,如果您在 viewDidLoad()
甚至 viewDidLayoutSubviews()
开始动画,屏幕还不可见 --- 所以您看不到初始图像(或者您只看到如果动画很短,请看得很简单。
所以,在 viewDidAppear()
开始你的动画,或者稍微延迟一下,如下所示:
class ViewController: UIViewController {
var bckgImageView = UIImageView(image: UIImage(named: "one"))
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(bckgImageView)
bckgImageView.translatesAutoresizingMaskIntoConstraints = false
// constrain image view to all 4 sides
NSLayoutConstraint.activate([
bckgImageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
bckgImageView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
bckgImageView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
bckgImageView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
])
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// start animation after 1 second
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
self.animateBackground()
})
}
func animateBackground() {
UIView.transition(with: self.view,
duration: 5,
options: .transitionCrossDissolve,
animations: { self.bckgImageView.image = UIImage(named: "two") },
completion: nil)
}
}
我试图通过使用 UIImageView 并通过设置过渡动画来在两个图像之间平滑过渡,但似乎过渡没有按预期进行,因为出现了白屏而不是新图像淡入。
我用了作为参考,不知道哪里出错了:(
class ViewController: UIViewController {
var bckgImageView = UIImageView(image: UIImage(named: "one"))
override func viewDidLoad() {
super.viewDidLoad()
bckgImageView.frame = self.view.frame
self.view.addSubview(bckgImageView)
animateBackground()
}
func animateBackground() {
UIView.transition(with: self.view,
duration: 5,
options: .transitionCrossDissolve,
animations: { self.bckgImageView.image = UIImage(named: "two") },
completion: nil)
}
在 viewDidLoad 中尚未计算 AutoLayout
您可以在viewDidLayoutSubviews 中更改布局时设置框架。 试试这个
class ViewController: UIViewController {
var bckgImageView = UIImageView(image: UIImage(named: "one"))
override func viewDidLoad() {
super.viewDidLoad()
bckgImageView.frame = self.view.frame
self.view.addSubview(bckgImageView)
animateBackground()
}
func animateBackground() {
UIView.transition(with: self.view,
duration: 5,
options: .transitionCrossDissolve,
animations: { self.bckgImageView.image = UIImage(named: "two") },
completion: nil)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
bckgImageView.frame = self.view.frame
}
}
首先,您要将图像视图的框架设置为 "root" 视图的框架,即在 viewDidLoad()
中定义的 not-yet。你最好使用约束并让 auto-layout 调整视图的大小。
其次,如果您在 viewDidLoad()
甚至 viewDidLayoutSubviews()
开始动画,屏幕还不可见 --- 所以您看不到初始图像(或者您只看到如果动画很短,请看得很简单。
所以,在 viewDidAppear()
开始你的动画,或者稍微延迟一下,如下所示:
class ViewController: UIViewController {
var bckgImageView = UIImageView(image: UIImage(named: "one"))
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(bckgImageView)
bckgImageView.translatesAutoresizingMaskIntoConstraints = false
// constrain image view to all 4 sides
NSLayoutConstraint.activate([
bckgImageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
bckgImageView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
bckgImageView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
bckgImageView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
])
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// start animation after 1 second
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
self.animateBackground()
})
}
func animateBackground() {
UIView.transition(with: self.view,
duration: 5,
options: .transitionCrossDissolve,
animations: { self.bckgImageView.image = UIImage(named: "two") },
completion: nil)
}
}