因此,您正在尝试使用点击手势更新 UIView 的高度。您需要以下IBOutlets:
-
NSLayoutConstraint
- 一个
CGFloat 变量来更新它,你
可以从您的视图控制器访问以更改您的
NSLayoutConstraint的常量
- 一个
UITapGestureRecognizer
此外,您的 UIImageView 需要位于其他视图的后面。您可以将它拖到 Interface Builder 中的视图层次结构的顶部。顶视图位于层次结构的后面:
这里是:
为视图和要设置动画的 UIView 的高度创建 IBOutlets:
@IBOutlet weak var bioView: UIView!
@IBOutlet weak var bioViewHeightConstraint: NSLayoutConstraint!
这只是对故事板的参考。如果你想改变bioViewHeightConstraint,你需要访问它的常量。所以为此声明一个变量:
var bioViewHeight: CGFloat! {
/*
Here we're using a property observer to execute code whenever
this value is updated
*/
didSet {
print("stackViewHeight updated to \(bioViewHeight)")
UIView.animate(withDuration: 0.5, delay: 0.0, options: [.curveEaseIn], animations: {
self.bioViewHeightConstraint.constant = self.bioViewHeight
self.view.layoutIfNeeded()
}) { _ in
self.bioTextView.scrollRangeToVisible(NSRange(location: 0, length: 0))
print("animation complete")
}
}
}
您会看到我在didSet 中放入了一些动画代码。因此,每当您更改 bioViewHeight 时,bioViewHeightConstraint.constant 都会更新。
接下来,我们配置一个UITapGestureRecognizer,代码如下:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(toggleHeight(sender:)))
tapGestureRecognizer.numberOfTapsRequired = 1
bioView.addGestureRecognizer(tapGestureRecognizer)
点击手势代码进入viewDidLoad。
接下来,我们必须为您的水龙头编写处理程序。这是代码:
func toggleHeight(sender: UITapGestureRecognizer) {
if bioViewHeightConstraint.constant == UIScreen.main.bounds.height / 2 {
// true
bioViewHeight = UIScreen.main.bounds.height - 30 // gives space for status bar
} else {
// false
bioViewHeight = UIScreen.main.bounds.height / 2
}
}
这是可以做你想做的事情的基本代码。这是一个指向repo 的链接以及完整的代码。