Event handling for iOS-how hitest:withEvent:and pointInside:withEvent:are related?


URL:http://stackoverflow.com/questions/4961386/event-handling-for-ios-how-hittestwithevent-and-pointinsidewithevent-are-r
The implement of  hitTest:withEvent: in UISponder does the follwing:
  • It cals  pointInside:withEvent: 保存先  self
  • If the return is NO,  hitTest:withEvent: returns  nil.the end of the story.
  • If the return is YES,it sends  hitTest:withEvent: messages to its subview.it starts from the top-level subview,and continues to other view until a subview returns a non-nil object,or all subview receive the message.
  • If a subview returns a non-nil object in the first time,the first  hitTest:withEvent: returns that object.the end of the story.
  • If no subview returns a non-nil object,the first  hitTest:withEvent: returns  self
  • This process repeat s recursively、so normally the leaf view of the view hierarchy is returned eventualy.
    However,you might override  hitTest:withEvent to do something differently.In many cases,overriding  pointInside:withEvent: is simpler and still provides enough options to tweak event handing in your appliation.
            I think you are confusing subclassing with the view hierarchy.What the doc says is as follows.Say You have this view hierarchy.By hierarchy I'm not talking aboot clashierarch y,but views withvillows.
    Event handling for iOS - how hitTest:withEvent: and pointInside:withEvent: are related?_第1张图片
    you put your finger inside  D.Here's what will happen:
  • hitTest:withEvent: is caled on  A,the top-most view of the view hierarchy.
  • pointInside:withEvent: is caled recursively on each view.
  • pointInside:withEvent: is caled on  A、and returns  YES
  • pointInside:withEvent: is caled on  B、and returns  NO
  • pointInside:withEvent: is caled on  C、and returns  YES
  • pointInside:withEvent: is caled on  D、and returns  YES
  • On the view that returned  YES,it will look down the hierarchy to see the subview where the touch took place.In this case,from  A、  C and  D、it will be  D.
  • D will be the hit-test view