UIView alpha 在第一次调用更改它后不会更新。 (Swift 4.2)
UIView alpha does not update after first call to change it. (Swift 4.2)
我有一个 UIView,当用户打开滑出菜单时,我想将其 alpha 从 0 更改为 0.5。当用户点击变暗区域时,alpha 应该返回到 0。目前,当点击菜单按钮时,alpha 变为 0.5,为视图添加暗淡效果。但是,断点和打印语句显示当点击 UIView 行以将 alpha 更改回 0 运行时,但 UI 仍然显示 0.5 alpha。我到处看代码都是完全一样的,所以我不确定我做错了什么。
let dimView = UIView()
func setupMenuButton() {
let menuButton = UIBarButtonItem(title: "Menu", style: .plain, target: self, action: #selector(showMenu))
navigationItem.rightBarButtonItem = menuButton
}
@objc func showMenu() {
//TODO: present menu and dim background
if let window = UIApplication.shared.keyWindow {
let dimView = UIView()
dimView.backgroundColor = UIColor.black
dimView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissDimView)))
window.addSubview(dimView)
dimView.frame = window.frame
dimView.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
dimView.alpha = 0.5
})
}
}
@objc func dismissDimView() {
UIView.animate(withDuration: 0.5, animations: {
self.dimView.alpha = 0
print("dim view is not transparent")
})
}
showMenu
中创建的dimView
与第一行中创建的dimView
不同。您正在 showMenu
中创建一个全新的 dimView
。
解决此问题的一种方法是不在 showMenu
中创建新的 dimView
,而是使用在外部声明的:
@objc func showMenu() {
//TODO: present menu and dim background
if let window = UIApplication.shared.keyWindow {
// notice I deleted a line here
dimView.backgroundColor = UIColor.black
dimView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissDimView)))
window.addSubview(dimView)
dimView.frame = window.frame
dimView.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
dimView.alpha = 0.5
})
}
}
@objc func dismissDimView() {
UIView.animate(withDuration: 0.5, animations: {
self.dimView.alpha = 0
// here I remove the dimView from the window so that it can be added back in the next time showMenu is called
}, completion: { [weak self] _ in self?.dimView.removeFromSuperView() })
}
我有一个 UIView,当用户打开滑出菜单时,我想将其 alpha 从 0 更改为 0.5。当用户点击变暗区域时,alpha 应该返回到 0。目前,当点击菜单按钮时,alpha 变为 0.5,为视图添加暗淡效果。但是,断点和打印语句显示当点击 UIView 行以将 alpha 更改回 0 运行时,但 UI 仍然显示 0.5 alpha。我到处看代码都是完全一样的,所以我不确定我做错了什么。
let dimView = UIView()
func setupMenuButton() {
let menuButton = UIBarButtonItem(title: "Menu", style: .plain, target: self, action: #selector(showMenu))
navigationItem.rightBarButtonItem = menuButton
}
@objc func showMenu() {
//TODO: present menu and dim background
if let window = UIApplication.shared.keyWindow {
let dimView = UIView()
dimView.backgroundColor = UIColor.black
dimView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissDimView)))
window.addSubview(dimView)
dimView.frame = window.frame
dimView.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
dimView.alpha = 0.5
})
}
}
@objc func dismissDimView() {
UIView.animate(withDuration: 0.5, animations: {
self.dimView.alpha = 0
print("dim view is not transparent")
})
}
showMenu
中创建的dimView
与第一行中创建的dimView
不同。您正在 showMenu
中创建一个全新的 dimView
。
解决此问题的一种方法是不在 showMenu
中创建新的 dimView
,而是使用在外部声明的:
@objc func showMenu() {
//TODO: present menu and dim background
if let window = UIApplication.shared.keyWindow {
// notice I deleted a line here
dimView.backgroundColor = UIColor.black
dimView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissDimView)))
window.addSubview(dimView)
dimView.frame = window.frame
dimView.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
dimView.alpha = 0.5
})
}
}
@objc func dismissDimView() {
UIView.animate(withDuration: 0.5, animations: {
self.dimView.alpha = 0
// here I remove the dimView from the window so that it can be added back in the next time showMenu is called
}, completion: { [weak self] _ in self?.dimView.removeFromSuperView() })
}