iOS/iOS Swift 개발 일기

토스트 메시지 띄우고 화면 전환 동시에 되는 경우

Developer-Michelle 2023. 9. 4. 09:47

토스트 메시지 띄우고 화면 전환 동시에 시켜야되는 경우가 있는데,

이 때 Navigation controller push 로 화면전환을 하면,

토스트메시지 띄우는 시간이 매우 짧고, (토스트 메시지 내용이 긴 경우) 토스트 메시지 다 읽기도 전에 화면 전환이 되어서 UX/UI 적으로 이상하게 보이는 경우가 있다.

 

이럴 때 토스트 메시지 시간을 1초 정도로 두고, 화면 전환하는 시간을 1.5초 정도로 두면,

사용자 입장에서 토스트메시지가 길어져도 다 읽고 다음 화면으로 넘어갈 수 있다.

 

[ Toast Message 띄우기 ]

    func showMessage(_ mesg: String, duration: TimeInterval = 1.0, completion: ((_ didTap: Bool) -> Void)?=nil) {
        self.hideToast()
        
        let sc = UIScreen.main.bounds
        let point = CGPoint(x: sc.size.width/2, y: sc.size.height - 150)
        self.view.makeToast(mesg, duration: duration, point: point, title: nil, image: nil, completion: completion)
    }
//토스트: 감추기
    func hideToast() {
        self.view.hideAllToasts()
    }

 

[ 화면 전환 ]

DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { //toast message 띄우는거 사용자에게 충분히 보여주고 넘어가기 위해서 화면 전환 시 1.5초 간격을 둠
            if let sb = self.storyboard,
               let vc = sb.instantiateViewController(withIdentifier: String(describing: JoinViewController.self)) as? JoinViewController {
                self.navigationController?.pushViewController(vc, animated: true)
                
            }
        }