天天看點

簡單區分UIResponder與UIControl

UIResponder類:上承NSObject,下接UIView ,UIVIewController ,UIApplacation;響應點,壓,滑;

UIControl類:上承UIView,下接UIButton等開關按鈕;

主要差別在于:

前者,主要是響應某個動作,執行某個行為--

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event;

後者,在繼承了前者的屬性基礎上,還能夠相應某個動作,為某個對象,添加動作--

- (void) addTarget:(id)target action:(SEL)action forControlEvents(UIControlEvents)controlEvents

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event 

傳回值:YES 接受使用者通過addTarget:action:forControlEvents添加的事件繼續處理。

傳回值:NO  則屏蔽使用者添加的任何事件 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 如果使用者重寫了該方法,則不會執行由使用者添加的其他事件,直接屏蔽了使用者的事件

觸摸與事件:

1、- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

   在UIView中判斷觸摸的點是否在UIView中起作用。可以實作一些非矩形的觸摸事件。

   例如:圓形,判斷點到圓心的距離。

   UIBezierPath,UITapGestureRecognizer等類是在sdk3.2及以後才可以使用的。

2、在UIWebView上的觸摸事件處理方法:

方法(1)繼承UIWindow,重寫- (void)sendEvent:(UIEvent *)event 

方法(2)使用UITapGestureRecognizer

                 UIGestureRecognizer   http://www.cnblogs.com/iphone520/archive/2011/10/27/2226548.html

3、響應鍊

  觸摸事件的響應是從view->viewcontroller->superview->superViewcontroller->window->application,如果一直沒有被處理,就被抛棄掉了。

  在ios4中,touch事件隻被最内層的視圖響應。

  但在ios5中,它可能從内層向外挨個響應。

  響應都鍊排列順序大緻與視圖層次結構順序相反。

4、分派事件

   使用下面兩個方法分派事件給響應者處理:

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event;

- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents;                        // send all actions associated with events

  事件與操作的差別:事件報告對螢幕的觸摸;操作報告對控件的操縱。

5、事件處理

   調用[self.nextResponder touchesBegan:touches withEvent:event];把事件傳遞

   參考 http://blog.csdn.net/iefreer/article/details/4754482

6、- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

     Returns the farthest descendant of the receiver in the view hierarchy (including itself) that contains a specified point.

     - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;

繼續閱讀