在视图上动画 UIIMageView Alpha 确实加载
Animating UIIMageView Alpha on view did load
我正在尝试为我的 HomeController
.
的背景图片制作动画
我想使图像淡入,因此我希望为它设置动画 alpha
属性。
我在这里找到了很多关于这个问题的答案,但是很多看起来都过时了(obj c,iOS 3/4/5 等)并且按照推荐的方法,我仍然无法让这个动画工作。
任何人都可以告诉我我的代码有什么问题吗?
我想制作动画 backgroundImage
。我已将动画代码放在 setupView
方法的末尾。
它并不像我希望的那样 'fade in',基本上 alpha 直接跳到 0.7
import UIKit
class HomeController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
private let backgroundImage: UIImageView = {
let image = UIImageView()
image.image = #imageLiteral(resourceName: "foggy").withRenderingMode(.alwaysOriginal)
image.contentMode = .scaleToFill
image.alpha = 0
image.isOpaque = false
return image
}()
private let currentLocation: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Southampton, UK", for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 24)
button.contentMode = .center
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(onPressLocation), for: .touchUpInside)
return button
}()
private let currentTemp: UIButton = {
let button = UIButton(type: .system)
button.setTitle("37°C", for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 54, weight: .thin)
button.contentMode = .center
button.setTitleColor(.white, for: .normal)
button.addTarget(self, action: #selector(onPressTemp), for: .touchUpInside)
return button
}()
}
extension HomeController {
@objc fileprivate func onPressLocation() -> Void {
print("Location pressed.")
}
@objc fileprivate func onPressTemp() -> Void {
self.currentTemp.setTitle("98°F", for: .normal)
}
fileprivate func setupView() -> Void {
view.backgroundColor = .white
// MARK: Background Image
view.addSubview(backgroundImage)
backgroundImage.anchor(
top: view.safeAreaLayoutGuide.topAnchor,
left: view.safeAreaLayoutGuide.leftAnchor,
bottom: view.safeAreaLayoutGuide.bottomAnchor,
right: view.safeAreaLayoutGuide.rightAnchor,
paddingTop: 0,
paddingLeft: 0,
paddingBottom: 0,
paddingRight: 0,
width: 0,
height: 0
)
// MARK: Current Location
view.addSubview(currentLocation)
currentLocation.anchor(
top: view.safeAreaLayoutGuide.topAnchor,
left: view.safeAreaLayoutGuide.leftAnchor,
right: view.safeAreaLayoutGuide.rightAnchor,
paddingTop: 50,
paddingLeft: 0,
paddingBottom: 0,
paddingRight: 0,
width: 0,
height: 26,
centerX: view.centerXAnchor
)
// MARK: Temp Button
view.addSubview(currentTemp)
currentTemp.anchor(
top: currentLocation.bottomAnchor,
paddingTop: 10,
paddingLeft: 0,
paddingBottom: 0,
paddingRight: 0,
width: 200,
height: 70,
centerX: view.centerXAnchor
)
// MARK: Animations
UIView.animate(withDuration: 6, delay: 3, options: .curveEaseInOut, animations: {
self.backgroundImage.alpha = 0.7
}, completion: nil)
}
}
IBOutlets
在调用 viewDidLoad
时不可见,因此请在 viewDidAppear
中调用 setupView
方法,该方法在您的视图出现在屏幕上后调用。
希望对您有所帮助。
您可以将您的代码放在 viewDidAppear 任何动画作品的视图生命周期方法中,因为 viewDidAppear 方法会通知视图控制器其视图已添加到视图中 hierarchy.Use 下面的代码为您的解决方案。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated: animated)
//Call method like this
setupView()
}
我正在尝试为我的 HomeController
.
我想使图像淡入,因此我希望为它设置动画 alpha
属性。
我在这里找到了很多关于这个问题的答案,但是很多看起来都过时了(obj c,iOS 3/4/5 等)并且按照推荐的方法,我仍然无法让这个动画工作。
任何人都可以告诉我我的代码有什么问题吗?
我想制作动画 backgroundImage
。我已将动画代码放在 setupView
方法的末尾。
它并不像我希望的那样 'fade in',基本上 alpha 直接跳到 0.7
import UIKit
class HomeController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
private let backgroundImage: UIImageView = {
let image = UIImageView()
image.image = #imageLiteral(resourceName: "foggy").withRenderingMode(.alwaysOriginal)
image.contentMode = .scaleToFill
image.alpha = 0
image.isOpaque = false
return image
}()
private let currentLocation: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Southampton, UK", for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 24)
button.contentMode = .center
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(onPressLocation), for: .touchUpInside)
return button
}()
private let currentTemp: UIButton = {
let button = UIButton(type: .system)
button.setTitle("37°C", for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 54, weight: .thin)
button.contentMode = .center
button.setTitleColor(.white, for: .normal)
button.addTarget(self, action: #selector(onPressTemp), for: .touchUpInside)
return button
}()
}
extension HomeController {
@objc fileprivate func onPressLocation() -> Void {
print("Location pressed.")
}
@objc fileprivate func onPressTemp() -> Void {
self.currentTemp.setTitle("98°F", for: .normal)
}
fileprivate func setupView() -> Void {
view.backgroundColor = .white
// MARK: Background Image
view.addSubview(backgroundImage)
backgroundImage.anchor(
top: view.safeAreaLayoutGuide.topAnchor,
left: view.safeAreaLayoutGuide.leftAnchor,
bottom: view.safeAreaLayoutGuide.bottomAnchor,
right: view.safeAreaLayoutGuide.rightAnchor,
paddingTop: 0,
paddingLeft: 0,
paddingBottom: 0,
paddingRight: 0,
width: 0,
height: 0
)
// MARK: Current Location
view.addSubview(currentLocation)
currentLocation.anchor(
top: view.safeAreaLayoutGuide.topAnchor,
left: view.safeAreaLayoutGuide.leftAnchor,
right: view.safeAreaLayoutGuide.rightAnchor,
paddingTop: 50,
paddingLeft: 0,
paddingBottom: 0,
paddingRight: 0,
width: 0,
height: 26,
centerX: view.centerXAnchor
)
// MARK: Temp Button
view.addSubview(currentTemp)
currentTemp.anchor(
top: currentLocation.bottomAnchor,
paddingTop: 10,
paddingLeft: 0,
paddingBottom: 0,
paddingRight: 0,
width: 200,
height: 70,
centerX: view.centerXAnchor
)
// MARK: Animations
UIView.animate(withDuration: 6, delay: 3, options: .curveEaseInOut, animations: {
self.backgroundImage.alpha = 0.7
}, completion: nil)
}
}
IBOutlets
在调用 viewDidLoad
时不可见,因此请在 viewDidAppear
中调用 setupView
方法,该方法在您的视图出现在屏幕上后调用。
希望对您有所帮助。
您可以将您的代码放在 viewDidAppear 任何动画作品的视图生命周期方法中,因为 viewDidAppear 方法会通知视图控制器其视图已添加到视图中 hierarchy.Use 下面的代码为您的解决方案。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated: animated)
//Call method like this
setupView()
}