我正在尝试将 UILabel 向左移动并为移动设置动画。 UILabel 是从 Storyboard创建的,并具有以下约束:
但是如果我试图使尾随空格常数等于 150 而不是 20 以将其向左移动,它会奇怪地向左弹跳一秒钟然后回到中间。所以我将左前导约束的优先级降低到 900,但同样的事情仍然发生。
所以我尝试了:
self.textToReadLabel.transform = CGAffineTransformMakeTranslation(-500.0, 0.0);
移动它,但如果我对它使用动画,它会滑出屏幕,滑回屏幕,然后停在它应该的位置......在所有这些戏剧之后。
我将如何实现这一点?
当我点击按钮移动标签时发生的全部事情:
self.textHorizontalPlacementConstraint.constant = 150.0;
self.textHorizontalPlacementConstraint2.constant = -110.0;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
}];
CATransition *animation = [CATransition animation];
animation.duration = 0.5;
animation.type = kCATransitionFade;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[self.textToReadLabel.layer addAnimation:animation forKey:@"changeTextTransition"];
// Change the text
self.textToReadLabel.text = @"text";
}
外观视频:
最佳答案
您可以在 Interface Builder 中设置约束的导出,然后当您想要为更改设置动画时,您可以按如下方式修改代码:
[UIView animateWithDuration:2.0 animations:^{
//Change your constraints from code.
}];
例如:如果我有一个 leftSpacing 约束,我可以直接如下更改它:
[UIView animateWithDuration:2.0 animations:^{
//Change your constraints from code.
self.leftSpacing.constant = self.leftSpacing.constant+20;
[self.view layoutIfNeeded];
}];
以上代码将使您的 View 从左到右移动 20 点。
更新:
您是否尝试过添加多个约束:比如间距值应小于或等于 say:200 但大于或等于 20。我认为这可能会有所帮助。
关于ios - 如何使用自动布局移动标签并为更改设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18164063/