animation - 约束动画未按预期动画
问题描述
以下代码用于将文本字段位置移动到应用程序上的编辑位置。不幸的是,它没有产生预期的效果。
textfield1 在 X 中移动到其完成位置,然后在 y 中移动到其完成位置。
我希望这两个约束都能产生文本字段的对角线运动,但在 iPhone 上测试时它不起作用。
动画有效,但我最终以倒置的“L”形运动而不是对角线“\”运动结束
任何如何解决此问题的建议将不胜感激。
-(void)textFieldEditingPosition
{
NSLog(@"editing");
[_regular setNeedsUpdateConstraints];
switch (_activeTextField.tag)
{
case 1:
self.textField2WidthConstraint.constant =_regular.frame.size.width/4 -16;
self.textField2XConstraint.constant = -_regular.frame.size.width/4 - self.textField2WidthConstraint.constant/2 -8;
self.textField2YConstraint.constant = -(_regular.frame.size.height/2 -_inputView.frame.size.height/2);
_textField1YConstraint.constant = 0;
_textField1XConstraint.constant = 0;
_textField1WidthConstraint.constant = _placeHolderTextfield.frame.size.width;
break;
case 2:
self.textField1XConstraint.constant = (_regular.frame.size.width /4) /2;
self.textField1YConstraint.constant = -(_regular.frame.size.height/8 -8) ;
self.textField1WidthConstraint.constant = _regular.frame.size.width/4 * 3 -16;
self.textField2YConstraint.constant =0;
self.textField2XConstraint.constant = 0;
self.textField2WidthConstraint.constant = _placeHolderTextfield.frame.size.width;
break;
default:
break;
}
[UIView animateWithDuration:0.7
delay: 0.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.view layoutIfNeeded];
}
completion:nil];
}
解决方案
我已经设法解决了这个问题,如下所示
首先,我在模拟器中打开了慢速动画。
这使我能够确切地看到正在发生的事情。我看到的 L 形运动实际上是由文本字段宽度随着文本字段移动到新的 x 和 y 位置而扩大引起的错觉。
扩大的宽度抵消了 xi 期待看到的运动。
通过将宽度扩展分离到动画完成中,我得到了我需要的结果,即文本字段到编辑位置的对角线移动。
文本字段宽度的扩展发生在没有动画的完成块中,这对我来说很好,因为文本字段背景是隐藏的,并且用户只能看到占位符文本。
特此修改后的代码:
-(void)textFieldEditingPosition
{
NSLog(@"editing");
[_regular setNeedsUpdateConstraints];
switch (_activeTextField.tag)
{
case 1:{
self.textField2WidthConstraint.constant =_regular.frame.size.width/4 -16;
self.textField2XConstraint.constant = -_regular.frame.size.width/4 - self.textField2WidthConstraint.constant/2 -8;
self.textField2YConstraint.constant = -(_regular.frame.size.height/2 -_inputView.frame.size.height/2);
_textField1YConstraint.constant = 0;
_textField1XConstraint.constant = 0;
[UIView animateWithDuration:0.7
delay: 0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^{
[self.regular layoutIfNeeded];
}
completion:^(BOOL finished){
self->_textField1WidthConstraint.constant = self->_placeHolderTextfield.frame.size.width;
[self.regular layoutIfNeeded];}];
}
break;
case 2:{
self.textField1XConstraint.constant = (_regular.frame.size.width /4) /2;
self.textField1YConstraint.constant = -(_regular.frame.size.height/8 -8) ;
self.textField1WidthConstraint.constant = _regular.frame.size.width/4 * 3 -16;
self.textField2YConstraint.constant =0;
self.textField2XConstraint.constant = 0;
[UIView animateWithDuration:0.7
delay: 0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^{
[self.regular layoutIfNeeded];
}
completion:^(BOOL finished){
self->_textField2WidthConstraint.constant = self->_placeHolderTextfield.frame.size.width;
[self.regular layoutIfNeeded];}];
}
break;
default:
break;
}
}
我敢肯定有一种更简洁的编码方式,但它确实有效,所以我松了一口气
谢谢
推荐阅读
- css - 带有引导程序 4 的溢出容器
- ibm-midrange - 调试器 API - 提交调试命令 (QteSubmitDebugCommand) API
- asp.net-core - 大文件(> 2 GB)的 ASP.NET 核心中的 FileStreamResult 导致 System.IO.IOException:流太长
- java - 如何在 java jar 文件中创建正确的资源文件路径
- node.js - 制作不和谐机器人(js)回显消息,但从消息中删除命令?
- java - 用于 GPA 不匹配的 Kotlin 正则表达式
- dart - 如何从 FireStore 排序列表
- c# - IRS-A2A App.config 中的无效子元素 gzipMessageEncoding
- r - 在 R 中创建一个介于开始日期和结束日期之间的每天的数据集
- delphi - 如何在 Delphi 10.2 Tokyo Update 1 中将 TPopupMenu 和 TMainMenu Behaive 更改为完全 RightToLeft?