SwiftUI Tap Gestures Not Responding in SwiftUI View After UIKit Interactive Transition Cancelled

I am using a custom UINavigationController with a custom pop animation and an interactive transition. The custom navigation controller works well for standard UIKit view controllers. However, when I push a UIHostingController that hosts a SwiftUI view, the SwiftUI view becomes unresponsive to tap gestures if the interactive transition is cancelled. The issue seems to occur specifically after the interactive transition is started and then cancelled.

This is interactive transition code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class CustomPopAnimator: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.3
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
guard let fromView = transitionContext.view(forKey: .from),
let toView = transitionContext.view(forKey: .to) else {
return
}
containerView.insertSubview(toView, belowSubview: fromView)
let screenWidth = UIScreen.main.bounds.width
toView.transform = CGAffineTransform(translationX: -screenWidth / 3, y: 0)
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
fromView.transform = CGAffineTransform(translationX: screenWidth, y: 0)
toView.transform = .identity
}) { finished in
fromView.transform = .identity
toView.transform = .identity
fromView.isUserInteractionEnabled = true
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
}
class CustomInteractiveTransition: UIPercentDrivenInteractiveTransition {
var hasStarted = false
var shouldFinish = false
override func cancel() {
super.cancel()
reset()
}
override func finish() {
super.finish()
reset()
}
private func reset() {
hasStarted = false
shouldFinish = false
}
}
@objc
class CustomNavigationController: UINavigationController, UINavigationControllerDelegate {
private let customAnimator = CustomPopAnimator()
private let customInteractiveTransition = CustomInteractiveTransition()
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
setupGesture()
}
private func setupGesture() {
let edgeSwipeGesture = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(handleEdgeSwipe(_:)))
edgeSwipeGesture.edges = .left
view.addGestureRecognizer(edgeSwipeGesture)
}
@objc private func handleEdgeSwipe(_ gesture: UIScreenEdgePanGestureRecognizer) {
let translation = gesture.translation(in: view)
let progress = translation.x / view.bounds.width
switch gesture.state {
case .began:
customInteractiveTransition.hasStarted = true
popViewController(animated: true)
case .changed:
customInteractiveTransition.shouldFinish = progress > 0.5
customInteractiveTransition.update(progress)
case .ended:
customInteractiveTransition.hasStarted = false
customInteractiveTransition.shouldFinish ? customInteractiveTransition.finish() : customInteractiveTransition.cancel()
case .cancelled:
customInteractiveTransition.hasStarted = false
customInteractiveTransition.cancel()
default:
break
}
}
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return operation == .pop ? customAnimator : nil
}
func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return customInteractiveTransition.hasStarted ? customInteractiveTransition : nil
}
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
if let hostingController = viewController as? AIMAHostingController<SwiftUIView> {
DispatchQueue.main.async {
hostingController.view.setNeedsLayout()
hostingController.view.layoutIfNeeded()
}
}
}
}
</code>
<code>class CustomPopAnimator: NSObject, UIViewControllerAnimatedTransitioning { func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { return 0.3 } func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { let containerView = transitionContext.containerView guard let fromView = transitionContext.view(forKey: .from), let toView = transitionContext.view(forKey: .to) else { return } containerView.insertSubview(toView, belowSubview: fromView) let screenWidth = UIScreen.main.bounds.width toView.transform = CGAffineTransform(translationX: -screenWidth / 3, y: 0) UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: { fromView.transform = CGAffineTransform(translationX: screenWidth, y: 0) toView.transform = .identity }) { finished in fromView.transform = .identity toView.transform = .identity fromView.isUserInteractionEnabled = true transitionContext.completeTransition(!transitionContext.transitionWasCancelled) } } } class CustomInteractiveTransition: UIPercentDrivenInteractiveTransition { var hasStarted = false var shouldFinish = false override func cancel() { super.cancel() reset() } override func finish() { super.finish() reset() } private func reset() { hasStarted = false shouldFinish = false } } @objc class CustomNavigationController: UINavigationController, UINavigationControllerDelegate { private let customAnimator = CustomPopAnimator() private let customInteractiveTransition = CustomInteractiveTransition() override func viewDidLoad() { super.viewDidLoad() delegate = self setupGesture() } private func setupGesture() { let edgeSwipeGesture = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(handleEdgeSwipe(_:))) edgeSwipeGesture.edges = .left view.addGestureRecognizer(edgeSwipeGesture) } @objc private func handleEdgeSwipe(_ gesture: UIScreenEdgePanGestureRecognizer) { let translation = gesture.translation(in: view) let progress = translation.x / view.bounds.width switch gesture.state { case .began: customInteractiveTransition.hasStarted = true popViewController(animated: true) case .changed: customInteractiveTransition.shouldFinish = progress > 0.5 customInteractiveTransition.update(progress) case .ended: customInteractiveTransition.hasStarted = false customInteractiveTransition.shouldFinish ? customInteractiveTransition.finish() : customInteractiveTransition.cancel() case .cancelled: customInteractiveTransition.hasStarted = false customInteractiveTransition.cancel() default: break } } func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { return operation == .pop ? customAnimator : nil } func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? { return customInteractiveTransition.hasStarted ? customInteractiveTransition : nil } func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) { if let hostingController = viewController as? AIMAHostingController<SwiftUIView> { DispatchQueue.main.async { hostingController.view.setNeedsLayout() hostingController.view.layoutIfNeeded() } } } } </code>
class CustomPopAnimator: NSObject, UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.3
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView
        guard let fromView = transitionContext.view(forKey: .from),
              let toView = transitionContext.view(forKey: .to) else {
            return
        }

        containerView.insertSubview(toView, belowSubview: fromView)

        let screenWidth = UIScreen.main.bounds.width
        toView.transform = CGAffineTransform(translationX: -screenWidth / 3, y: 0)

        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            fromView.transform = CGAffineTransform(translationX: screenWidth, y: 0)
            toView.transform = .identity
        }) { finished in
            fromView.transform = .identity
            toView.transform = .identity
            fromView.isUserInteractionEnabled = true
        
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
        }
    }
}


class CustomInteractiveTransition: UIPercentDrivenInteractiveTransition {
    var hasStarted = false
    var shouldFinish = false

    override func cancel() {
        super.cancel()
        reset()
    }

    override func finish() {
        super.finish()
        reset()
    }

    private func reset() {
        hasStarted = false
        shouldFinish = false
    }
}


@objc
class CustomNavigationController: UINavigationController, UINavigationControllerDelegate {
    private let customAnimator = CustomPopAnimator()
    private let customInteractiveTransition = CustomInteractiveTransition()

    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
        setupGesture()
    }

    private func setupGesture() {
        let edgeSwipeGesture = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(handleEdgeSwipe(_:)))
        edgeSwipeGesture.edges = .left
        view.addGestureRecognizer(edgeSwipeGesture)
    }

    @objc private func handleEdgeSwipe(_ gesture: UIScreenEdgePanGestureRecognizer) {
        let translation = gesture.translation(in: view)
        let progress = translation.x / view.bounds.width

        switch gesture.state {
        case .began:
            customInteractiveTransition.hasStarted = true
            popViewController(animated: true)
        case .changed:
            customInteractiveTransition.shouldFinish = progress > 0.5
            customInteractiveTransition.update(progress)
        case .ended:
            customInteractiveTransition.hasStarted = false
            customInteractiveTransition.shouldFinish ? customInteractiveTransition.finish() : customInteractiveTransition.cancel()
        case .cancelled:
            customInteractiveTransition.hasStarted = false
            customInteractiveTransition.cancel()
        default:
            break
        }
    }

    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return operation == .pop ? customAnimator : nil
    }

    func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        return customInteractiveTransition.hasStarted ? customInteractiveTransition : nil
    }

    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        if let hostingController = viewController as? AIMAHostingController<SwiftUIView> {
            DispatchQueue.main.async {
                hostingController.view.setNeedsLayout()
                hostingController.view.layoutIfNeeded()
            }
        }
    }
}

SwiftUI code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import SwiftUI
struct SwiftUIView: View {
var body: some View {
VStack {
Spacer()
Text("Hello, World!")
.id("123")
.background(Color.green)
.padding()
.onTapGesture {
print("===onclick")
}
Spacer()
}
}
}
</code>
<code>import SwiftUI struct SwiftUIView: View { var body: some View { VStack { Spacer() Text("Hello, World!") .id("123") .background(Color.green) .padding() .onTapGesture { print("===onclick") } Spacer() } } } </code>
import SwiftUI

struct SwiftUIView: View {
    var body: some View {
        VStack {
            Spacer()
            Text("Hello, World!")
                .id("123")
                .background(Color.green)
                .padding()
                .onTapGesture {
                    print("===onclick")
                }
            Spacer()
        }
    }
}

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> let hostingController = UIHostingController(rootView: SwiftUIView())
navigationController?.pushViewController(hostingController, animated: true)
</code>
<code> let hostingController = UIHostingController(rootView: SwiftUIView()) navigationController?.pushViewController(hostingController, animated: true) </code>
 let hostingController = UIHostingController(rootView: SwiftUIView())
        navigationController?.pushViewController(hostingController, animated: true)

New contributor

user25329204 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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