自定义VC过渡无法正确消除

时间:2019-11-22 07:11:42

标签: ios swift

我正在尝试通过自定义转换在VC中重新创建库存UIAlertAction。目前,我的演示文稿可以完美地工作(backgroundView逐渐淡出,“ notification” VC幻灯片从底部滑出)。我面临的问题是当我关闭VC时backgroundView不会消失。好像它完全绕过了我的动画块。一旦completeTransition被调用,backgroundView就会完全消失。怎么了?

class AnimationController: NSObject {
 let backgroundView = UIView()
}

extension AnimationController:  UIViewControllerAnimatedTransitioning {
  func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    let containerView = transitionContext.containerView

    guard let toViewController = transitionContext.viewController(forKey: .to),
        let fromViewController = transitionContext.viewController(forKey: .from) else {
            transitionContext.completeTransition(false)
        return
    }
    switch animationType {
    case .present:
        backgroundView.frame = CGRect(x: 0, y: 0, width: toViewController.view.frame.width, height: fromViewController.view.frame.height)
        backgroundView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4)
        backgroundView.alpha = 0
        containerView.addSubview(backgroundView)

        let viewToAnimate = toViewController.view!
        containerView.addSubview(viewToAnimate)

        toViewController.view.clipsToBounds = true

        viewToAnimate.frame = CGRect(x: 0, y: viewToAnimate.frame.maxY, width: viewToAnimate.frame.width, height: viewToAnimate.frame.height)

        let duration = transitionDuration(using: transitionContext)

        UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.80, initialSpringVelocity: 0.1, options: .curveEaseInOut, animations:  {
            self.backgroundView.alpha = 1.0
            viewToAnimate.transform = CGAffineTransform(translationX: 0, y: -viewToAnimate.frame.height)
        }) { _ in
            transitionContext.completeTransition(true)
        }
    case .dismiss:

        containerView.addSubview(fromViewController.view!)

        let testView = fromViewController.view!

        backgroundView.frame = CGRect(x: 0, y: 0, width: testView.frame.width, height: testView.frame.height)
        backgroundView.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1)

        let duration = transitionDuration(using: transitionContext)

        UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.80, initialSpringVelocity: 0.1, options: .curveEaseInOut, animations:  {
            self.backgroundView.alpha = 0.0
            testView.transform = CGAffineTransform(translationX: 0, y: testView.frame.height)
            print("backgroundView Doesn't fade out")
        }) { _ in
            print("backgroundView disappears here")
            transitionContext.completeTransition(true)
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您可以尝试这样显示

location /admin {
    if (!-e $request_filename) {
        rewrite (.*)$ /admin/index.php last;
    }
}

您可以稍后使用

将其关闭
//where DevolucionVC is you ViewController 
    if let controller = DevolucionVC() as? DevolucionVC {
                if let window = self.window, let rootViewController = window.rootViewController {
                    var currentController = rootViewController
                    while let presentedController = currentController.presentedViewController {
                        currentController = presentedController
                    }
                    currentController.present(controller, animated: true, completion: nil)
                }
            }

答案 1 :(得分:0)

尝试一下

目前,替换此代码

    backgroundView.frame = CGRect(x: 0, y: 0, width: toViewController.view.frame.width, height: fromViewController.view.frame.height)
    backgroundView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4)
    backgroundView.alpha = 0
    containerView.addSubview(backgroundView)

    let transition = CATransition()
    transition.duration = 0.5
    transition.type = CATransitionType.moveIn
    transition.subtype = CATransitionSubtype.fromTop
    containerView.layer.add(transition, forKey: kCATransition)

要关闭,请替换此代码

  containerView.addSubview(fromViewController.view!)
  let transition = CATransition()
  transition.duration = 0.5
  transition.type = CATransitionType.fade
  containerView.layer.add(transition, forKey: kCATransition)
  backgroundView.frame = CGRect(x: 0, y: 0, width: testView.frame.width, height: testView.frame.height)
  backgroundView.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1)
  self.backgroundView.alpha = 0.0

答案 2 :(得分:0)

您的问题是您要处理(SELECT Key1, Key2, Key3, SUM(Value1) AS Sum1, SUM(Value2) AS Sum2 FROM TABLE1_DETAILED GROUP BY Key1, Key2, Key3) as TABLE1_SUMS 的两个单独实例,因此要处理public class ItemAdapter : RecyclerView.Adapter<ItemAdapter.BaseViewHolder<ListItem>>的两个单独实例。实例化一个实例进行演示,而另一个实例化进行解雇。请注意,在您的public abstract class BaseViewHolder<in T>(view: View) : RecyclerView.ViewHolder(view){}块中,您正在呼叫AnimationController,但在backgroundView块中却没有这样做。结果,case .present:块中的containerView.addSubview(backgroundView)实例甚至都不是视图层次结构的一部分。

您需要做的是在第一个动画完成块中调用case .dismiss:。然后,在您的backgroundView中,您需要再次致电case .dismiss:

尝试一下...

self.backgroundView.removeFromSuperview()