Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - swift] long touch 시 개발자모드 불러오기 본문

iOS 응용 (swift)

[iOS - swift] long touch 시 개발자모드 불러오기

jake-kim 2021. 3. 22. 23:17

debug를 위해서 아무 화면 long touch시 개발자 모드 로드 원리

  • AppDelegate에서 window객체에 longTouchRecogniger 등록하여 구현
  • 설명에 편의를 위해 SceneDelegate삭제

구현 방법

  • application(:didFinishLaunchingWithOptions:)에서 longPressed등록
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        registerLongPressed()
        return true
    }
    
    private func registerLongPressed() {
        // TODO 
    }

}
  • 구현: window에 addGestureRecogniger()를 사용
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        registerLongPressed()
        return true
    }

    private func registerLongPressed() {
        #if DEBUG
        let longPressed = UILongPressGestureRecognizer(target: self, action: #selector(showVC))
        longPressed.minimumPressDuration = 1.0
        window?.addGestureRecognizer(longPressed)
        #endif
    }

    @objc private func showVC(gesture: UILongPressGestureRecognizer) {
        if gesture.state == .began {
            let vc = LongPressedVC(nibName: "LongPressedVC", bundle: nil)
            vc.modalPresentationStyle = .overCurrentContext
            window?.rootViewController?.present(vc, animated: true, completion: nil)
        }
    }

}

 

Comments