停止UIView动画或检测视图是否为动画

时间:2014-05-30 07:11:06

标签: ios objective-c animation uiview

有一个带有uiviews的数组。它们的帧由于当前interfaceOrientation

而设置

然后我们使用

以编程方式将这些视图添加到self.view
-(void)placeView
{
    if ([self.arrayWithViews count] > 0)
    {
        [UIView animateWithDuration:1.f animations:^{
            UIView *currentView = [self.arrayWithViews lastObject];
            [currentView setAlpha:0.f];
            [self.view addSubview:currentView];
            [currentView setAlpha:1.f];
            [self.arrayWithViews removeLastObject];
        } completion:^(BOOL finished) {
            [self placeView];
        }];
    }
}

现在,如果方向发生变化,视图将继续添加(坐标对应于前一个方向)。

问题是 - 如何停止动画处理或如何在动画发生时防止界面方向改变。

P.S。 [self.view.layer removeAllAnimations]不起作用(我没有弄清楚原因)

你能告诉我该怎么做吗?

1 个答案:

答案 0 :(得分:1)

removeAllAnimations仍然会调用completion阻止,因此您重复调用placeView,因此您需要添加一个标记:

-(void)placeView
    {
        if(!self.runAni){ //Add a flag to stop repeat to call [self placeView]
            return;
        }
        if ([self.arrayWithViews count] > 0)
        {
            [UIView animateWithDuration:1.f animations:^{
                UIView *currentView = [self.arrayWithViews lastObject];
                [currentView setAlpha:0.f];
                [self.view addSubview:currentView];
                [currentView setAlpha:1.f];
                [self.arrayWithViews removeLastObject];
            } completion:^(BOOL finished) {
                [self placeView];
            }];
        }
    }

要停止动画,你应该这样做:

self.runAni = NO;
[self.view.layer removeAllAnimations];

开始做:

self.runAni = YES;
[self placeView];