안드로이드 터치이벤트 더블탭, 트리플탭 구분

나고수·2022년 9월 4일
0

1일1공부

목록 보기
65/67

더블탭은 GestureDetector의 onDoubleTap()으로 쉽게 구현할 수 있다.
트리플탭은 따로 제공되는 함수가 없는 듯하여 직접 구현했다.

  • 기본 원리 :
    handler로 n초 동안 postdelay를 준다.
    그 사이에 터치 이벤트가 3번이 되면 특정행동 실행
    n초 이후에도 터치이벤트가 1번이면 runnable내의 if (touchCount == 1)가 실행
  private var touchCount: Int = 0      //터치 누적 횟수
  private var DELAY: Long = 400    //handler delay

 binding.root.setOnClickListener {
            touchCount++    //화면 터치 시, 터치 횟수 증가
            Log.d("LOGGING", "$touchCount")

            var handler = Handler(Looper.getMainLooper())
            handler.postDelayed({
                // delay 초가 지나고 난 후
                kotlin.run {
                    if (touchCount == 1) {// 터치 이벤트가 1번 이면
                        Log.d("LOGGING", "touchCount 1")
                    }
                    //delay초 이후에 터치이벤트가 3번 이상이 아니면 다 들어옴 > 터치이벤트 횟수 초기화
                    //if (touchevent==1) 안에서 터치이벤트를 리셋 시키면, delay초 이후에 터치이벤트가 2번일때 touchevent를 0 으로 초기화 시켜주는 부분이 없다. 
                    //그래서 2번 터치 후 시간이 지나고 또 한번 터치하면 사용자는 touchevent ==1 일때의 기능을 원하겠지만, 실제로는 touchevnet ==3 일때가 작동된다.
                    //그래서 if (touchevent==1) 구문 밖에 적어서 touchevent가 1,2일 때 모두 초기화 해줘야함.
                    touchCount = 0
                }
            }, DELAY)

            if (touchCount == 3) {  //delay 초가 지나기 전에 touch횟수가 3번이면
                touchCount = 0
                Log.d("LOGGING", "touchCount 3")
            }

        }
profile
되고싶다

0개의 댓글