简体   繁体   English

动画时再次加载时,自定义UIView不会从超级视图中删除

[英]Custom UIView doesn't get removed from superview when loaded again while animating

The question explains it all actually. 问题实际上解释了这一切。

I load a custom UIView on top of my current view, animated, using this code: 我使用以下代码在当前视图之上加载了一个自定义UIView动画:

- (void)showView
{
    self.blurView.alpha = 0.f;

    [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^
     {
         self.blurView.alpha = 1.f;
     } completion:^(BOOL finished) {
         [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^
          {
              self.blurView.alpha = 0.f;
          } completion:nil];
     }];
}

It works, but when I perform -(void)showView again while it is still animating, the custom view doesn't get removed from the screen. 它可以工作,但是当我仍在设置动画时再次执行-(void)showView ,自定义视图不会从屏幕上删除。

It just stays like this: 它只是这样保留:

在此处输入图片说明

I figured out what the problem was. 我发现了问题所在。

blurView is a public variable and every time I did -(void)showView it used the same variable, setting the alpha to 0.f just once, thus never changing the alpha of the first show. blurView是一个公共变量,每次我执行-(void)showView它都使用相同的变量,仅将alpha设置为0.f,因此永远不会更改第一个节目的alpha。

I declared the variable blurView in the method itself and now it works because every blurView gets it's own pointer. 我在方法本身中声明了变量blurView ,现在它可以工作,因为每个blurView都获得了自己的指针。

-(void)someMethod
{
    //Create a live blur view
    FXBlurView *blurView = [[FXBlurView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    blurView.center = CGPointMake(self.view.window.frame.size.width / 2, self.view.window.frame.size.height / 2);
    blurView.layer.cornerRadius = 20.f;
    [self.view.window addSubview:blurView];

    [self showView:(blurView)];
}

- (void)showView:(FXBlurView *)blurView
{
    //Make full transparent before showing
    blurView.alpha = 0.f;

    //Start animation
    [FXBlurView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^
     {
         blurView.alpha = 1.f;
     } completion:^(BOOL finished) {
         if (finished)
         {
             //End animation
             [FXBlurView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^
              {
                  blurView.alpha = 0.f;
              } completion:nil];
         }
     }];
}

You should add to the top of your function this line: 您应该将此行添加到函数顶部:

[self.view.layer removeAllAnimations];

It will remove all of the active animations. 它将删除所有活动的动画。

It doesn't work because you have implemented a delay, use animation block with delay - it will create animation keys that could be removed with this function above. 它不起作用,因为您已实现了延迟,请使用具有延迟的动画块-它会创建可以通过上面的此功能删除的动画关键点。

[UIView animateWithDuration:0.3 delay:0.0
                    options:UIViewAnimationOptionAllowUserInteraction
                 animations:^
 {
     // ...
 } completion:NULL];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM