【问题标题】:Animating removeFromSuperview动画 removeFromSuperview
【发布时间】:2011-01-01 12:53:24
【问题描述】:

我想制作从子视图到超级视图的过渡动画。

我使用以下方式显示子视图:

[UIView beginAnimations:@"curlup" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[self.view addSubview:self.mysubview.view];
[UIView commitAnimations];

以上工作正常。它回到我没有得到任何动画的超级视图:

[UIView beginAnimations:@"curldown" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];

我应该做些什么不同的事情来让子视图在删除时进行动画处理?

【问题讨论】:

    标签: ios iphone cocoa-touch uiview core-animation


    【解决方案1】:

    如果你的目标是 iOS 4.0 以上,你可以使用动画块来代替:

    [UIView animateWithDuration:0.2
         animations:^{view.alpha = 0.0;}
         completion:^(BOOL finished){ [view removeFromSuperview]; }];
    

    (以上代码来自Apple's UIView documentation

    【讨论】:

    • 唉!我一直认为 删除 需要 0.2 秒。但是删除是瞬间发生的。 alpha 变化,需要 0.2 秒。如果我们再次将其设置为 200 秒,从超级视图中删除仍然是瞬时的,但它不会发生,直到 200 秒过去...
    【解决方案2】:

    下面的例子:

    func removeSpinningGear(cell: LocalSongsCollectionViewCell) {
        UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveLinear, animations: {
            cell.spinningGearBtn.alpha = 0.0
        }) { _ in
            cell.spinningGearBtn.removeFromSuperview()
        }
    }
    

    【讨论】:

      【解决方案3】:

      虽然从动画完成块发送removeFromSuperview 消息的方法在大多数情况下都可以正常工作,但有时无法阻止视图立即从视图层次结构中删除。

      例如,MKMapView 在收到消息 removeAnnotations 后会删除其子视图,并且 API 中没有此消息的“动画”替代方案。

      尽管如此,以下代码允许您在视图从超级视图中删除甚至解除分配后,对视图的可视化克隆做任何您想做的事情:

      UIView * snapshotView = [view snapshotViewAfterScreenUpdates:NO];
      snapshotView.frame = view.frame;
      [[view superview] insertSubview:snapshotView aboveSubview:view];
      
      // Calling API function that implicitly triggers removeFromSuperview for view
      [mapView removeAnnotation: annotation];
      
      // Safely animate snapshotView and release it when animation is finished
      [UIView animateWithDuration:1.0
                           snapshotView.alpha = 0.0;
                       }
                       completion:^(BOOL finished) {
                           [snapshotView removeFromSuperview];
                       }];
      

      【讨论】:

        【解决方案4】:

        JosephH 用 Swift 回答:

        UIView.animateWithDuration(0.2, animations: {view.alpha = 0.0}, 
                                        completion: {(value: Bool) in
                                                      view.removeFromSuperview()
                                                    })
        

        【讨论】:

          【解决方案5】:

          我认为您需要改为使用forView:self.view.superview,以与您在添加时所做的保持一致,因为在这种情况下,self.view 是孩子,因此您需要在父母身上进行.

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-10-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-01-21
            • 2012-08-13
            • 2021-12-20
            相关资源
            最近更新 更多