             self.animationView?.alpha = 0
             completion?()
         })
-
+        
         let firstItemView = animationView?.subviews.filter { $0.tag == 0 }.first
         firstItemView?.layer.cornerRadius = 0
         firstItemView?.layer.masksToBounds = true

        }
        return durations
    }

    func openAnimation(_ completion: (() -> Void)?) {
        isUnfolded = true
        removeImageItemsFromAnimationView()
        addImageItemsToAnimationView()

        animationView?.alpha = 1
        containerView.alpha = 0

        let durations = durationSequence(.open)

        var delay: TimeInterval = 0
        var timing = kCAMediaTimingFunctionEaseIn
        var from: CGFloat = 0.0
        var to: CGFloat = -CGFloat.pi / 2
        var hidden = true
        configureAnimationItems(.open)

        guard let animationItemViews = self.animationItemViews else {
            return
        }

        for index in 0 ..< animationItemViews.count {
            let animatedView = animationItemViews[index]

            animatedView.foldingAnimation(timing, from: from, to: to, duration: durations[index], delay: delay, hidden: hidden)

            from = from == 0.0 ? CGFloat.pi / 2 : 0.0
            to = to == 0.0 ? -CGFloat.pi / 2 : 0.0
            timing = timing == kCAMediaTimingFunctionEaseIn ? kCAMediaTimingFunctionEaseOut : kCAMediaTimingFunctionEaseIn
            hidden = !hidden
            delay += durations[index]
        }

        let firstItemView = animationView?.subviews.filter { $0.tag == 0 }.first

        firstItemView?.layer.masksToBounds = true
        DispatchQueue.main.asyncAfter(deadline: .now() + durations[0], execute: {
            firstItemView?.layer.cornerRadius = 0
        })

        DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
            self.animationView?.alpha = 0
            self.containerView.alpha = 1
            completion?()
        }
    }

    func closeAnimation(_ completion: (() -> Void)?) {
        isUnfolded = false
        removeImageItemsFromAnimationView()
        addImageItemsToAnimationView()

        guard let animationItemViews = self.animationItemViews else {
            fatalError()
        }

        animationView?.alpha = 1
        containerView.alpha = 0

        var durations: [TimeInterval] = durationSequence(.close).reversed()

        var delay: TimeInterval = 0
        var timing = kCAMediaTimingFunctionEaseIn
        var from: CGFloat = 0.0
        var to: CGFloat = CGFloat.pi / 2
        var hidden = true
        configureAnimationItems(.close)

        if durations.count < animationItemViews.count {
            fatalError("wrong override func animationDuration(itemIndex:NSInteger, type:AnimationType)-> NSTimeInterval")
        }
        for index in 0 ..< animationItemViews.count {
            let animatedView = animationItemViews.reversed()[index]

            animatedView.foldingAnimation(timing, from: from, to: to, duration: durations[index], delay: delay, hidden: hidden)

            to = to == 0.0 ? CGFloat.pi / 2 : 0.0
            from = from == 0.0 ? -CGFloat.pi / 2 : 0.0
            timing = timing == kCAMediaTimingFunctionEaseIn ? kCAMediaTimingFunctionEaseOut : kCAMediaTimingFunctionEaseIn

open class RotatedView: UIView {
    var hiddenAfterAnimation = false
    var backView: RotatedView?
    
    func addBackView(_ height: CGFloat, color: UIColor) {
        let view = RotatedView(frame: CGRect.zero)
        view.backgroundColor = color
