【发布时间】:2015-05-22 05:28:51
【问题描述】:
我一直在努力停止/删除启动后的无限约束动画。基本上我有一个与主视图具有相等高度约束的框,乘数为 0.5。该框应该通过将乘数更改为 0.8 并通过 UIViewAnimationOptionAutoreverse | 更改为 0.5 来增大和缩小。 UIViewAnimationOptionRepeat 。由于 multiplier 是只读的,因此对其进行动画处理的唯一方法是删除约束并将其与 a multiplier 一起添加回来。该框的动画效果很好,但使用经典方法停止 UIView 动画,停止方法似乎根本不起作用。
如果我为 box.transofrm = CGAffineTransformMakeScale(1.0f, 0.8f) 设置动画,我可以停止动画,但不建议在启用自动布局时这样做而不是为约束本身设置动画。
这是我的代码
// wired equal height constraint between box and view with multiplier 0.5
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *boxEqualHeightConstraint;
.
-(void)startAnimation {
// animation starts fine
[self.view removeConstraint:self.boxEqualHeightConstraint]; // remove multi 0.5
self.boxEqualHeightConstraint = [NSLayoutConstraint constraintWithItem:self.box
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.8
constant:0];
[self.view addConstraint:self.boxEqualHeightConstraint]; // add multi 0.8
[self.view layoutIfNeeded];
[UIView animateWithDuration:1.0f delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
[self.view layoutIfNeeded];
} completion:nil];
}
-(void)stopAnimations {
[self.view.layer removeAllAnimations]; // not working
[self.box.layer removeAllAnimations]; // not working
// I tried replacing the constraint animation, but it is still growing and shrinking indefinitely.
[self.view removeConstraint:self.boxEqualHeightConstraint];
self.boxEqualHeightConstraint = [NSLayoutConstraint constraintWithItem:self.box
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0];
[self.view addConstraint:self.boxEqualHeightConstraint];
[self.view layoutIfNeeded];
[UIView animateWithDuration:1.0f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[self.view layoutIfNeeded];
} completion:nil];
}
【问题讨论】:
-
[self.box.layer removeAllAnimations]为我工作。您确定正在调用stopAnimations吗? -
@rdelmar 是的,我确定。我得到的唯一效果是盒子坐标发生了变化,盒子将不再围绕视图中心进行动画处理,但它仍然在动画化(增长>收缩)。 [self.box animationKeys] 返回 bounds.size 的键,该键被 removeAllAnimations 删除,但由于某种原因,这只会改变框的位置。
-
删除动画块之前的
[self.view layoutIfNeeded];,除此之外它可以工作,停止突然跳跃,但这是一个不同的问题。您也许应该看看更改优先级,而不是一直添加和删除约束。
标签: ios cocoa-touch autolayout