如何知道UIView动画何时全部完成 [英] How to know when UIView animations have all completed

查看:67
本文介绍了如何知道UIView动画何时全部完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的自定义视图正通过其容器进行动画处理-在我的视图中,我用其他动画(例如UICOllectionView)更新了子视图的某些部分

A custom view I created is being animated by its container - in my view I update portions of the subviews with other animations (for instance a UICOllectionView)

,这会引发错误

*** Assertion failure in -[UICollectionView _endItemAnimations

在四周转转,我发现我的视图具有附加的动画:

Digging around I see that my view has animations attached to it:

<GeneratorView: 0x15b45950; frame = (0 0; 320 199); animations = { position=<CABasicAnimation: 0x145540a0>; }; layer = <CALayer: 0x15b49410>>

所以现在在执行动画操作之前,我要检查:

So now before performing animation operations I check:

NSArray *animationKeys = [self.layer animationKeys];
    for (NSString *key in animationKeys) {
        CAAnimation *animation = [self.layer animationForKey:key];

    }

我看到返回了动画对象:

and I see that animation objects are returned:

这时我想等所有动画完成后再更新自我。

at this point I would like to "wait" until all animations have completed before updating self.

我看到我可以将我自己添加为CAAnimation委托。

I see that I can add myself as the CAAnimation delegate.

但这有点混乱且难以跟踪。

But this is a bit messy and hard to track.

是有没有更简单的方法可以使用UIView方法-更高级别?

Is there an easier way using a UIView method - much higher level?

推荐答案

您可以使用UIView方法在容器中制作动画:

You can use UIView method to do the animation in the container:

[UIView animateWithDuration: animations: completion:];
[UIView animateWithDuration: delay: options: animations: completion:];

您应在自定义视图中添加属性:

You should add properties to your custom view:

@property BOOL isAnimating;

在容器中运行动画块并更改isAnimating属性的视图。
此方法accept块将在动画完成时触发。
示例:

In the container run the animation block and change view isAnimating property. This method accept block which will be fired when the animation complete. Example:

[UIView animateWithDuration:1.0 animations:^{
        //Your animation block
        view1.isAnimating = YES;
        view1.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
        // etc.
    }completion:^(BOOL finished){
        view1.isAnimating = NO;
        NSLog(@"Animation completed.");
    }];

现在,您可以在视图中查看该视图是否为动画:

Now it your view you can see if the view is animating:

if (self.isAnimating)
    NSLog(@"view is animating");

这篇关于如何知道UIView动画何时全部完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆