why is SwiftUI view slide transition isn’t working as expected?

I would like a SwiftUI slide transition effect in which the current UIView slides out and next UIView slides in, but the current UIView simply disappears and then the next view slides in, couldn’t find out the reason, I’m using Xcode 14.2 for iOS 16.2.

class SlideTransition: NSObject, UIViewControllerAnimatedTransitioning {
    enum Direction {
        case left, right
    }

    let duration: TimeInterval
    let direction: Direction

    init(duration: TimeInterval = 1, direction: Direction = .left) {
        self.duration = duration
        self.direction = direction
        super.init()
    }

    @objc func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }
    
    @objc func animateTransition(using 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
        
        // Determine the slide direction
        switch direction {
        case .left:
            fromViewEndFrame = containerBounds.offsetBy(dx: -containerBounds.width, dy: 0)
            toViewStartFrame = containerBounds.offsetBy(dx: containerBounds.width, dy: 0)
        case .right:
            fromViewEndFrame = containerBounds.offsetBy(dx: containerBounds.width, dy: 0)
            toViewStartFrame = containerBounds.offsetBy(dx: -containerBounds.width, dy: 0)
        }

        toView.frame = toViewStartFrame
        
        containerView.addSubview(toView)
        
//        UIView.animate(withDuration: duration, animations: {
//            // Slide out the fromView and slide in the toView
//            fromView.frame = fromViewEndFrame
//            toView.frame = containerBounds
//        }, completion: { finished in
//            let wasCancelled = transitionContext.transitionWasCancelled
//            transitionContext.completeTransition(!wasCancelled)
//            
//            // If the transition was cancelled, remove the toView to avoid a broken state
//            if wasCancelled {
//                toView.removeFromSuperview()
//            }
//        })
        
//        Perform the animation
        let animator = UIViewPropertyAnimator(duration: transitionDuration(using: transitionContext), curve: .easeInOut) {
            // Animate both views simultaneously
            fromView.frame = fromViewEndFrame
            toView.frame = containerBounds
        }

        // Add a completion block to handle the end of the animation
        animator.addCompletion { finished in
            let wasCancelled = transitionContext.transitionWasCancelled
            transitionContext.completeTransition(!wasCancelled)

            // If the transition was canceled, reset the toView's frame to its initial position
            if wasCancelled {
                toView.removeFromSuperview()
            }
        }

        animator.startAnimation()
    }
}


both UIView.animate and UIViewPropertyAnimator have the same results, the current view simply disappears instead of sliding out.

And I tried the HeroTransition library, the transition worked, but unable to set the transition duration.

could someone help? thanks 🙂

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật