2012년 6월 5일 화요일

Android onInterceptTouchEvent함수 설명의 번역


onInterceptTouchEvent (MotionEvent ev) 함수 설명 번역. 


Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.
Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:
  1. You will receive the down event here.
  2. The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
  3. For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
  4. If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.
Parameters
evThe motion event being dispatched down the hierarchy.
Returns
  • Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent(). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.
모든 터치 동작 이벤트들을 가로채기 위한다면 이 함수를 구현하면된다. 이 함수를 구현하면 당신은 당신의 자식(뷰)들에게 이벤트가 보내지는 것을 지켜 볼 수 있다. 그리고 어느 시점이든 현재 제스처에 대한 소유권을 가질 수 있다. 
이 함수를 사용할 때는 onTouchEvent(MotionEvent) 함수와 상당히 복잡한 상호작용을 하기 때문에 주의해야 한다. 또한 onTouchEvent()함수를 올바른 방법으로 구현해 주어야 한다. 이벤트는 다음과 같은 순서로 전달될 것이다. 
  1. down이벤트를 여기서 받을 것이다. 
  2. down이벤트는 이 뷰 그룹의 차일드에게, 아니면 onTouchEvent()에게 처리되도록 전달된다. 이 말인즉슨, 남은 제스처를 계속 보기 위해서는 onTouchEvent()함수에서 true값을 리턴하도록 구현해야 한다는 것을 의미한다. (대신 부모 뷰가 이것을 처리하도록 할 수도 있다. ) 또한, onTouchEvent()함수에서 true를 리턴하게 함으로서, onInterceptTouchEvent() 함수에서 따라오는 모든 이벤트들을 받지 않게 할 수 있으며, 모든 터치 처리는 onTouchEvent에서 정상적으로 처리되도록 할 수 있다. 
  3. 이 함수에서 false를 리턴하는 한, 각 따라오는 이벤트들(마지막 up이벤트까지)은 제일 먼저 이 함수로 배달될 것이며, 그 다음에 타겟의 onTouchEvent()함수로 전달될 것이다. 
  4. 만약 true를 이 함수에서 리턴한다면, 따라오는 어떠한 이벤트들도 받지 못할 것이다: 타겟 뷰는 같은 이벤트를 받겠지만 ACTION_CANCEL 액션도 받을 것이다. 그리고 그 이후의 이벤트들은 onTouchEvent()에만 전달되고 더이상 이 함수에 나타나지 않을 것이다. 
Parameters
ev계층적으로 전달되는 모션 이벤트. 
Returns
  • true를 리턴하면 자식들로부터의 이벤트를 훔치고 이 viewGroup으로 onTouchEvent함수를 통해 이벤트들이 전달되도록 한다. 현재 타겟은 ACTION_CANCEL 이벤트를 받게 될 것이고 더이상 어떠한 메시지들도 이 함수에 전달되지 않을 것이다. 
  • 역주) false를 리턴하면 계속 이 함수로 이벤트를 받아서 검사하겠다는 의미. 


ViewPager.java 에서의 onInterceptTouchEvent (MotionEvent ev)
이 함수는 단지 모션을 가로챌 것인지 말 것인지를 결정한다. 만약 true를 리턴하면 onMotionEvent함수가 불리게 될 것이며 실질적인 스크롤링을 그곳에서 할 것이다. 
이 함수가 하는 짓은 다음과 같다. 

  1. 전달된 Action이 ACTION_CANCEL 또는 ACTION_UP 과 같다면, 드래그가 끝난 것이므로 멤버변수들을 초기화하고 false를 리턴한다. 
  2. 만약 전달된 이벤트가 ACTION_DOWN이 아니라면, 
    1. 만약 지금 드래깅 중이면 true를 리턴
    2. 만약 드래그를 할 수 없는 상태라면 false를 리턴
  3. 케이스 ACTION_MOVE: 







댓글 없음:

댓글 쓰기