Computer >> 컴퓨터 >  >> 프로그램 작성 >> IOS

iOS에서 길게 누르기를 감지하는 방법은 무엇입니까?

<시간/>

길게 누르기(길게 누르기라고도 함) 제스처는 오랜 시간 동안 화면을 터치하는 하나 이상의 손가락을 감지합니다. 누름을 인식하는 데 필요한 최소 시간과 손가락이 화면을 터치해야 하는 횟수를 구성합니다. (제스쳐 인식기는 터치의 지속 시간에 의해서만 트리거되고 터치와 관련된 힘에 의해 트리거되지 않습니다.) 길게 누르기 제스처를 사용하여 누르고 있는 개체에 대한 작업을 시작할 수 있습니다. 예를 들어 상황에 맞는 메뉴를 표시하는 데 사용할 수 있습니다.

자세한 내용은 https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/handling_uikit_gestures/handling_long-press_gestures

를 참조하세요.

여기서 우리는 버튼을 일정 시간 동안 누르고(길게 누름) 경고를 표시하는 간단한 애플리케이션을 디자인할 것입니다.

시작하겠습니다.

1단계 − Xcode 열기 → New Projecr → Single View Application → 이름을 "LongPressGesture"로 지정합니다.

2단계 − Main.storyboard에서 버튼을 하나 추가하고 @IBOutlet을 만들고 이름을 "btnLongOutlet"으로 지정합니다.

3단계 − 이제 ViewController.swift를 열고 UILongPressGestureRecognizer()의 객체를 생성합니다.

var longgesture = UILongPressGestureRecognizer

4단계 − viewDidLoad()에서 다음 코드를 추가합니다.

longgesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPress(_:)))
longgesture.minimumPressDuration = 2
btnLongOutlet.addGestureRecognizer(longgesture)

5단계 − longPress 함수를 만들고 아래 코드 추가,

@objc func longPress(_ sender: UILongPressGestureRecognizer) {
   let alertController = UIAlertController(title: "Long Press", message:
      "Long Press Gesture Detected", preferredStyle: .alert)
      alertController.addAction(UIAlertAction(title: "OK", style: .default,handler: nil))
   present(alertController, animated: true, completion: nil)
}

6단계 − 그리고 완료되었습니다. 애플리케이션을 실행하고 버튼을 2초 동안 탭했는지 확인하세요.

iOS에서 길게 누르기를 감지하는 방법은 무엇입니까?