iOS使用者點擊事件處理

來源:互聯網
上載者:User

iOS使用者點擊事件處理
處理機制

iOS事件處理,首先應該是找到能處理點擊事件的視圖,然後在找到的這個視圖裡處理這個點擊事件。

處理原理如下:

• 當使用者點擊螢幕時,會產生一個觸摸事件,系統會將該事件加入到一個由UIApplication管理的事件隊列中

• UIApplication會從事件隊列中取出最前面的事件進行分發以便處理,通常,先發送事件給應用程式的主視窗(UIWindow)

• 主視窗會調用hitTest:withEvent:方法在視圖(UIView)階層中找到一個最合適的UIView來處理觸摸事件

(hitTest:withEvent:其實是UIView的一個方法,UIWindow繼承自UIView,因此主視窗UIWindow也是屬於視圖的一種)

• hitTest:withEvent:方法大致處理流程是這樣的:

首先調用當前視圖的pointInside:withEvent:方法判斷觸摸點是否在當前視圖內:

? 若pointInside:withEvent:方法返回NO,說明觸摸點不在當前視圖內,則當前視圖的hitTest:withEvent:返回nil

? 若pointInside:withEvent:方法返回YES,說明觸摸點在當前視圖內,則遍曆當前視圖的所有子視圖(subviews),調用子視圖的hitTest:withEvent:方法重複前面的步驟,子視圖的遍曆順序是從top到bottom,即從subviews數組的末尾向前遍曆,直到有子視圖的hitTest:withEvent:方法返回非Null 物件或者全部子視圖遍曆完畢:

? 若第一次有子視圖的hitTest:withEvent:方法返回非Null 物件,則當前視圖的hitTest:withEvent:方法就返回此對象,處理結束

? 若所有子視圖的hitTest:withEvent:方法都返回nil,則當前視圖的hitTest:withEvent:方法返回當前視圖自身(self)

• 最終,這個觸摸事件交給主視窗的hitTest:withEvent:方法返回的視圖對象去處理。我的號iOS開發:iOSDevTip

案列分析

在UIViewController的self.view上載入一個LGFirstView

LGFirstView上面有一個UIButton我們叫它buttonFirst

然後,self.view上載入一個LGSecondView,剛好蓋在LGFirstView上面

LGSecondView上面也有一個UIButton我們叫它buttonSecond

正常情況下:

使用者點擊LGSecondView(點擊的點不在buttonSecond上,但是在buttonFirst撒很難過嗎),事件處理流程如下:

1)調用UIWindow的hitTest:withEvent:方法,hitTest:withEvent:方法會調用pointInside:withEvent:方法。此時pointInside:withEvent:返回YES,說明觸摸事件在UIWindow上面。

2)去遍曆UIWindow上面的子視圖,也就是self.view。同樣也是調用self.view的hitTest:withEvent:方法,hitTest:withEvent:方法會調用pointInside:withEvent:方法。此時pointInside:withEvent:返回YES,說明觸摸事件在self.view上面。

3)去遍曆self.view上的子視圖,也就是LGFirstView和LGSecondView。(注意:子視圖的遍曆順序是從top到bottom,即從subviews數組的末尾向前遍曆)。

4)所以先調用LGSecondView的hitTest:withEvent:方法,hitTest:withEvent:方法會調用pointInside:withEvent:方法。此時pointInside:withEvent:返回YES,說明觸摸事件在LGSecondView上面。(此時不會再去遍曆LGFirstView,所以正如你所願buttonFirst的點擊事件也不會被調用)

5)還沒有結束,接著回去遍曆LGSecondView上的所有子視圖,結果所有子視圖的hitTest:withEvent:方法都返回nil,因為LGSecondView上面只有secondButton,而點擊的點不在secondButton。

6)最終hitTest:withEvent:方法返回當前視圖自身(self),而LGSecondView沒有事件要處理。整個過程結束。

如果我們想讓buttonFirst也響應點擊事件怎麼辦?方法一:

我們在LGSecondView加入如下代碼:

 

 

#pragma mark - 方法一-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event{    UIView *hitView = [super hitTest:point withEvent:event];    if (hitView == self)    {        return nil;    }    else    {        return hitView;    }}

 

我們再來分析一下:

還是這個情境,使用者點擊LGSecondView(點擊的點不在buttonSecond上,但是在buttonFirst撒很難過嗎),事件處理流程如下:

1)調用UIWindow的hitTest:withEvent:方法,hitTest:withEvent:方法會調用pointInside:withEvent:方法。此時pointInside:withEvent:返回YES,說明觸摸事件在UIWindow上面。

2)去遍曆UIWindow上面的子視圖,也就是self.view。同樣也是調用self.view的hitTest:withEvent:方法,hitTest:withEvent:方法會調用pointInside:withEvent:方法。此時pointInside:withEvent:返回YES,說明觸摸事件在self.view上面。

3)去遍曆self.view上的子視圖,也就是LGFirstView和LGSecondView。(注意:子視圖的遍曆順序是從top到bottom,即從subviews數組的末尾向前遍曆)。

4)所以先調用LGSecondView的hitTest:withEvent:方法,hitTest:withEvent:方法會調用pointInside:withEvent:方法。此時pointInside:withEvent:返回YES,說明觸摸事件在LGSecondView上面。

5)但是,注意了,這裡有個但是, UIView *hitView = [super hitTest:point withEvent:event];就是這句代碼發揮了作用。如果hitView是LGSecondView的話,就不處理點擊事件。(這跟userInteractionEnabled=NO是不一樣的,userInteractionEnabled=NO,LGSecondView上的buttonSecond也不會響應點擊事件了。)

6)這個時候會去調用LGFirstView的hitTest:withEvent:方法,hitTest:withEvent:方法會調用pointInside:withEvent:方法。此時pointInside:withEvent:返回YES,說明觸摸事件在LGFirstView上面。

7)再去遍曆LGFirstView上面的子視圖,也就是buttonFirst,調用buttonFirst的hitTest:withEvent:方法,hitTest:withEvent:方法會調用pointInside:withEvent:方法。此時pointInside:withEvent:返回YES,說明觸摸事件在buttonFirst上面。

8)再去遍曆buttonFirst上的所有子視圖,結果所有子視圖的hitTest:withEvent:方法都返回nil,說明點擊就在buttonFirst,buttonFirst就用響應的點擊方法。

方法二

在LGSecondView.m

@interface LGSecondView ()

@property (nonatomic, strong) NSMutableArray *subControlsArray;

@end

@implementation LGSecondView


- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.subControlsArray = [NSMutableArray array];
}
return self;
}


#pragma mark - 方法二

- (void)addSubview:(UIView *)view{
[super addSubview:view];
if ([view isKindOfClass:[UIControl class]]) {
[self.subControlsArray addObject:view];
}
}

//set self not response action and self subviews response action
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
{
BOOL toNext = YES;
for (UIView *view in _subControlsArray) {
if (CGRectContainsPoint(view.frame, point)) {
toNext = NO;
break;
}
}
return !toNext;
}

具體原理就不在累述了,大家自己推一下,也可以把你的思路寫下來發給我。

還有很多方法也歡迎你把思路寫下來發給我。我的號iOS開發:iOSDevTip

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.