舉例講解iOS應(yīng)用開發(fā)中hitTest觸摸事件的編寫方法
hitTest:withEvet 調(diào)用過程
比如如果是當(dāng)前的View A, 還有一個(gè)viewB
如果不重寫 hitTest 方法,那么 系統(tǒng)默認(rèn)是先調(diào)用viewA的hitest 方法,然后再調(diào)用viewB的htest方法。
系統(tǒng)的調(diào)用過程,跟下面的重寫hitest的方法是一模一樣的。
-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if ([self pointInside:point withEvent:event]) {
}
else {
return nil;
}
for (UIView *subView in self.subviews) {
if ([subView hitTest:point withEvent:event]!=nil) {
return subView;
}
}
return self;
}
在說明一次,如果不重寫hitest方法,那么每一個(gè)UIVIeew的默認(rèn)hitest的方法都是上面這個(gè)處理流程。
那也沒啥好說的。
但是對于一些特殊的處理過程,就不行了
所以之所以重寫hitTest方法,通常都是為了穿透上層 的 UIview,讓touch事件可以達(dá)到下面的uiview,
比如 view A 和 VIew B,
View b完全擋住了view A,但是我想讓點(diǎn)擊viewB的時(shí)候,view A可以響應(yīng)點(diǎn)擊的事件。就可以采用下面的方法:
-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if ([self pointInside:point withEvent:event]) {
NSLog(@"in view A");
return self;
}
else {
return nil;
}
}
深入
我們來更深入一下,現(xiàn)在有個(gè)實(shí)例需求界面如下,
Window
-ViewA
-ButtonA
-ViewB
-ButtonB
層次結(jié)構(gòu):ViewB完全蓋住了ButtonA,ButtonB在ViewB上,現(xiàn)在需要實(shí)現(xiàn):
(1)ButtonA和ButtonB都能響應(yīng)消息 (2)ViewA也能收到ViewB所收到的touches消息 (3)不讓ViewB(ButtonB)收到消息。
(首先解析下,默認(rèn)情況下,點(diǎn)擊了ButtonB的區(qū)域,iOS消息處理過程。
-ViewA
-ButtonA
-ViewB
-ButtonB
當(dāng)點(diǎn)擊ButtonB區(qū)域后,處理過程:從ViewA開始依次調(diào)用hitTest
pointInside的值依次為:
ViewA:NO;
ViewB:YES;
ButtonB:YES;
ButtonB的subViews:NO;
所以ButtonB的subViews的hitTest都返回nil,于是返回的處理對象是ButtonB自己。接下去開始處理touches系列方法,這里是調(diào)用ButtonB綁定的方法。處理完后消息就停止,整個(gè)過程結(jié)束。)
分析:
實(shí)現(xiàn)的方式多種,這里將兩個(gè)需求拆解開來實(shí)現(xiàn),因?yàn)閷?shí)現(xiàn)2就可以滿足1。
需求1的實(shí)現(xiàn),ViewB蓋住了ButtonA,所以默認(rèn)情況下ButtonA收不到消息,但是在消息機(jī)制里尋找消息響應(yīng)是從父View開始,所以我們可以在ViewA的hitTest方法里做判斷,若touch point是在ButtonA上,則將ButtonA作為消息處理對象返回。
代碼如下:
#pragma mark - hitTest
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// 當(dāng)touch point是在_btn上,則hitTest返回_btn
CGPoint btnPointInA = [_btn convertPoint:point fromView:self];
if ([_btn pointInside:btnPointInA withEvent:event]) {
return _btn;
}
// 否則,返回默認(rèn)處理
return [super hitTest:point withEvent:event];
}
這樣,當(dāng)觸碰點(diǎn)是在ButtonA上時(shí),則touch消息就被攔截在ViewA上,ViewB就收不到了。然后ButtonA就收到touch消息,會觸發(fā)onClick方法。
需求2的實(shí)現(xiàn),上面說到響應(yīng)鏈,ViewB只要override掉touches系列的方法,然后在自己處理完后,將消息傳遞給下一個(gè)響應(yīng)者(即父View即ViewA)。
代碼如下:在ViewB代碼里
#pragma mark - touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"B - touchesBeagan..");
// 把事件傳遞下去給父View或包含他的ViewController
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"B - touchesCancelled..");
// 把事件傳遞下去給父View或包含他的ViewController
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"B - touchesEnded..");
// 把事件傳遞下去給父View或包含他的ViewController
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"B - touchesMoved..");
// 把事件傳遞下去給父View或包含他的ViewController
[self.nextResponder touchesBegan:touches withEvent:event];
}
然后,在ViewA上就可以接收到touches消息,在ViewA上寫:
#pragma mark - touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"A - touchesBeagan..");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"A - touchesCancelled..");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"A - touchesEnded..");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"A - touchesMoved..");
}
這樣就實(shí)現(xiàn)了向父View透傳消息。
不讓ViewB收到消息,可以設(shè)置ViewB.UserInteractionEnable=NO;除了這樣還可以override掉ViewB的ponitInside,原理參考上面。
在ViewB上寫:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
// 本View不響應(yīng)用戶事件
return NO;
}
相關(guān)文章
iOS實(shí)現(xiàn)UITableView數(shù)據(jù)為空時(shí)的提示頁面
最近工作中遇到一個(gè)需求,當(dāng)UITableView數(shù)據(jù)為空的時(shí)候,給出一個(gè)簡單的提示頁面,通過從網(wǎng)上查找解決的方法,發(fā)現(xiàn)了兩種實(shí)現(xiàn)的方法,現(xiàn)在分享給大家,有需要的朋友們可以參考借鑒,下面感興趣的朋友們來一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11iOS 中 使用UITextField格式化銀行卡號碼的解決方案
今天小編給大家分享ios中使用UITextField格式化銀行卡號碼的實(shí)現(xiàn)思路詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-12-12全面解析iOS中同步請求、異步請求、GET請求、POST請求
通過本文給大家全面解析了iOS中同步請求、異步請求、GET請求、POST請求,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-08-08iOS實(shí)現(xiàn)選項(xiàng)卡效果的方法
選項(xiàng)卡在我們?nèi)粘i_發(fā)的時(shí)候經(jīng)常要用到,所以這篇文章給大家分享一種iOS實(shí)現(xiàn)的簡單選項(xiàng)卡效果,很適合大家學(xué)習(xí)和使用,有需要的可以參考借鑒,下面來一起看看吧。2016-09-09