Thursday, September 11, 2025
HomeiOS Developmentios - why is SwiftUI view slide transition is not working as...

ios – why is SwiftUI view slide transition is not working as anticipated?


I would really like a SwiftUI slide transition impact by which the present UIView slides out and subsequent UIView slides in, however the present UIView merely disappears after which the subsequent view slides in, could not discover out the rationale, I am utilizing Xcode 14.2 for iOS 16.2.

class SlideTransition: NSObject, UIViewControllerAnimatedTransitioning {
    enum Path {
        case left, proper
    }

    let length: TimeInterval
    let path: Path

    init(length: TimeInterval = 1, path: Path = .left) {
        self.length = length
        self.path = path
        tremendous.init()
    }

    @objc func transitionDuration(utilizing transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return length
    }
    
    @objc func animateTransition(utilizing transitionContext: UIViewControllerContextTransitioning) {
        guard
            let fromView = transitionContext.view(forKey: .from),
            let toView = transitionContext.view(forKey: .to)
        else {
            transitionContext.completeTransition(false)
            return
        }
        
        let containerView = transitionContext.containerView
        let containerBounds = containerView.bounds

        let fromViewEndFrame: CGRect
        let toViewStartFrame: CGRect
        
        // Decide the slide path
        swap path {
        case .left:
            fromViewEndFrame = containerBounds.offsetBy(dx: -containerBounds.width, dy: 0)
            toViewStartFrame = containerBounds.offsetBy(dx: containerBounds.width, dy: 0)
        case .proper:
            fromViewEndFrame = containerBounds.offsetBy(dx: containerBounds.width, dy: 0)
            toViewStartFrame = containerBounds.offsetBy(dx: -containerBounds.width, dy: 0)
        }

        toView.body = toViewStartFrame
        
        containerView.addSubview(toView)
        
//        UIView.animate(withDuration: length, animations: {
//            // Slide out the fromView and slide within the toView
//            fromView.body = fromViewEndFrame
//            toView.body = containerBounds
//        }, completion: { completed in
//            let wasCancelled = transitionContext.transitionWasCancelled
//            transitionContext.completeTransition(!wasCancelled)
//            
//            // If the transition was cancelled, take away the toView to keep away from a damaged state
//            if wasCancelled {
//                toView.removeFromSuperview()
//            }
//        })
        
//        Carry out the animation
        let animator = UIViewPropertyAnimator(length: transitionDuration(utilizing: transitionContext), curve: .easeInOut) {
            // Animate each views concurrently
            fromView.body = fromViewEndFrame
            toView.body = containerBounds
        }

        // Add a completion block to deal with the top of the animation
        animator.addCompletion { completed in
            let wasCancelled = transitionContext.transitionWasCancelled
            transitionContext.completeTransition(!wasCancelled)

            // If the transition was canceled, reset the toView's body to its preliminary place
            if wasCancelled {
                toView.removeFromSuperview()
            }
        }

        animator.startAnimation()
    }
}


each UIView.animate and UIViewPropertyAnimator have the identical outcomes, the present view merely disappears as an alternative of sliding out.

And I attempted the HeroTransition library, the transition labored, however unable to set the transition length.

does anybody know the best way to repair it?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments