停止无限约束动画

时间:2015-05-22 05:28:51

标签: ios cocoa-touch autolayout

我一直在努力在启动后停止/删除无限约束动画。基本上我有一个与主视图具有相同高度约束的框,乘数为0.5。通过将乘数更改为0.8并通过 UIViewAnimationOptionAutoreverse |更改为0.5来增加和缩小该框。 UIViewAnimationOptionRepeat 。由于乘数是只读的,因此设置动画的唯一方法是删除约束并使用乘数将其添加回来。该框动画效果很好,但停止方法似乎根本不能使用经典的方法来停止UIView动画。

enter image description here

如果我动画 box.transofrm = CGAffineTransformMakeScale(1.0f,0.8f),我可以停止动画,但不建议在启用自动布局时执行此操作而不是动画约束本身。< / p>

这是我的代码

// 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];

}

1 个答案:

答案 0 :(得分:0)

我发现动画的原因是什么,不停止。我的self.box(它是一个UIButton)包含一个图像。

在removeAllAnimations之后仍然是动画的只是框内的图像,而不是框本身。

所以要阻止它这样做:

[self.box.imageView.layer removeAllAnimations];