UIView - 带有持续时间的动画似乎忽略了持续时间
UIView - Animate with duration seems to ignore duration
我遇到了一个问题:我有一个非常简单的自定义 UIView
,它将一组 UITextField
和一组 UILabel
。
UILabel
在 UITextField
上,我希望当用户开始编辑 UITextField
时 UILabel
增加到 40px。
我正在使用约束来设置文本字段的框架和标签的框架。
所以我只是在自定义视图中使用 UITextFieldDelegate
协议设置委托,并实现委托方法:textFieldDidBeginEditing
,如下所示:
我只是在委托方法中为我的标签的约束(中心 Y)设置动画。
func textFieldDidBeginEditing(textField: UITextField) {
self.centerVerticallyLabelConstraint.constant = -40
UIView.animateWithDuration(0.5) {
self.textFieldLabel.layoutIfNeeded()
}
}
发生的事情是 UILabel
正确上升,但它立即上升。它忽略了我动画中 0.5
的持续时间。我不知道为什么:
我没有同时制作其他动画,我只有这个动画。
我没有为 UIView.
禁用动画
委托设置为我的 UITextfield
.
有什么想法吗?
尝试在您的视图上调用 layoutIfNeeded 而不是 textview。
UIView.animate(withDuration: 0.5) {
self.view.layoutIfNeeded()
}
希望对您有所帮助。
尝试执行以下代码:
func textFieldDidBeginEditing(textField: UITextField) {
UIView.animate(withDuration: 0.5) {
self.textFieldLabel.frame.origin.y -= 40
}
}
需要调用 self.view.layoutIfNeeded()
而不是 self.textFieldLabel.layoutIfNeeded()
谢谢
对我来说,问题似乎是因为我已经在主线程上并且我在 GCD main.async 调用中调用了动画块。
即我有
DispatchQueue.main.async {
<# update the constraint s#>
self.animatedViewsParent.setNeedsLayout()
animatedView.setNeedsLayout()
UIView.animate(withDuration: 2.0) {
self.animatedViewsParent.layoutIfNeeded()
}
}
视图动画移动但不动画逐渐超过 2.0 秒。删除外部 DispatchQueue.main.async {}
解决了问题。
我遇到了一个问题:我有一个非常简单的自定义 UIView
,它将一组 UITextField
和一组 UILabel
。
UILabel
在 UITextField
上,我希望当用户开始编辑 UITextField
时 UILabel
增加到 40px。
我正在使用约束来设置文本字段的框架和标签的框架。
所以我只是在自定义视图中使用 UITextFieldDelegate
协议设置委托,并实现委托方法:textFieldDidBeginEditing
,如下所示:
我只是在委托方法中为我的标签的约束(中心 Y)设置动画。
func textFieldDidBeginEditing(textField: UITextField) {
self.centerVerticallyLabelConstraint.constant = -40
UIView.animateWithDuration(0.5) {
self.textFieldLabel.layoutIfNeeded()
}
}
发生的事情是 UILabel
正确上升,但它立即上升。它忽略了我动画中 0.5
的持续时间。我不知道为什么:
我没有同时制作其他动画,我只有这个动画。
我没有为 UIView.
禁用动画
委托设置为我的 UITextfield
.
有什么想法吗?
尝试在您的视图上调用 layoutIfNeeded 而不是 textview。
UIView.animate(withDuration: 0.5) {
self.view.layoutIfNeeded()
}
希望对您有所帮助。
尝试执行以下代码:
func textFieldDidBeginEditing(textField: UITextField) {
UIView.animate(withDuration: 0.5) {
self.textFieldLabel.frame.origin.y -= 40
}
}
需要调用 self.view.layoutIfNeeded()
而不是 self.textFieldLabel.layoutIfNeeded()
谢谢
对我来说,问题似乎是因为我已经在主线程上并且我在 GCD main.async 调用中调用了动画块。
即我有
DispatchQueue.main.async {
<# update the constraint s#>
self.animatedViewsParent.setNeedsLayout()
animatedView.setNeedsLayout()
UIView.animate(withDuration: 2.0) {
self.animatedViewsParent.layoutIfNeeded()
}
}
视图动画移动但不动画逐渐超过 2.0 秒。删除外部 DispatchQueue.main.async {}
解决了问题。