我使用下面的代码显示并隐藏底部的子视图----->顶部或顶部---->底部带 UIViewAnimation
-(IBAction)showClander
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:NO];
[UIView setAnimationDuration:0.50];
pickerView.frame = CGRectMake(0, 568-368, pickerView.frame.size.width, pickerView.frame.size.height);
[UIView commitAnimations];
}
-(void)hideClander
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:NO];
[UIView setAnimationDuration:0.50];
pickerView.frame = CGRectMake(0, pickerView.frame.origin.y+368, pickerView.frame.size.width, pickerView.frame.size.height);
[UIView commitAnimations];
}
上面的代码在我没有使用Autolayout时工作正常,但因为我的应用支持纵向和横向,所以我应该使用自动布局。
现在问题是如何为这个移动视图设置不同的约束,对于静态视图我设置了不同的约束,这对我来说很好,如下面的截图所示
如上面的屏幕截图显示(Y值为371),当我尝试将其更改为 568 (这是我的要求)时,我没有得到结果截图。
所以问题是有没有办法使用现有的约束来移动对象而不做很多改变来实现上述结果?任何建议将不胜感激。谢谢提前
答案 0 :(得分:1)
您可以为约束本身的更改设置动画。更改需要更改的约束的constant
值,并在动画块内调用layoutIfNeeded
。
我自己的代码示例:
NSLayoutConstraint* con = self.v_verticalPositionConstraint;
con.constant -= 100;
[UIView animateWithDuration:1 animations:^{
[self.v layoutIfNeeded];
} completion:nil];
然而,一般情况下,动画和自动布局是敌人,因为他们试图以两种不同的方式改变视图的框架。请参阅我的书中的讨论:http://www.apeth.com/iOSBook/ch17.html#_animation_and_autolayout