NSLayoutConstraint约束动画实现
在用NSLayoutConstraint设置完约束,使用
[UIView animateWithDuration:0.25 animations:^{
self.headerSizeHeightConstraint.constant = self.headerViewHeight;
[self layoutIfNeeded];
}];
更新约束时,如果约束内有个上边距top设置默认是0,
所以view默认的frame的y也是0,在动画更新时会发现动画会从y是0的位置开始发生动画,不是当前的所在位置.
所以如果在约束top在某些时候有改变,用layoutIfNeeded更新约束动画时就会出现起始位置错误的动画过程.
目前解决办法我是把frame的y在动画更新之前设置为目前的新数值
self.headerView.frame = CGRectMake(0, self.headerOriginYConstraint.constant, SYS_WIDTH, oldHeaderHeight);
[UIView animateWithDuration:0.25 animations:^{
self.headerSizeHeightConstraint.constant = self.headerViewHeight;
[self layoutIfNeeded];
}];
这样约束动画开始时,起始位置就不会发生错误.
后记
还没有查清楚为什么约束动画会出现这种情况这么,我尝试设置动画属性为UIViewAnimationOptionBeginFromCurrentState,还是会发生动画起始位置有错误,目前解决办法感觉只是治标不治本,应该还是自己的写法有错误,希望知道的朋友说下真正的原因.
网友评论