国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

舉例講解iOS應(yīng)用開發(fā)中hitTest觸摸事件的編寫方法

2020-07-26 03:22:57
字體:
供稿:網(wǎng)友

 hitTest:withEvet  調(diào)用過程

比如如果是當(dāng)前的View A, 還有一個viewB

如果不重寫 hitTest 方法,那么 系統(tǒng)默認(rèn)是先調(diào)用viewA的hitest 方法,然后再調(diào)用viewB的htest方法。

系統(tǒng)的調(diào)用過程,跟下面的重寫hitest的方法是一模一樣的。

復(fù)制代碼 代碼如下:

-(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方法,那么每一個UIVIeew的默認(rèn)hitest的方法都是上面這個處理流程。

那也沒啥好說的。

但是對于一些特殊的處理過程,就不行了

所以之所以重寫hitTest方法,通常都是為了穿透上層 的 UIview,讓touch事件可以達(dá)到下面的uiview,

比如 view A  和 VIew B,

View b完全擋住了view A,但是我想讓點(diǎn)擊viewB的時候,view A可以響應(yīng)點(diǎn)擊的事件。就可以采用下面的方法:

復(fù)制代碼 代碼如下:

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

    if ([self pointInside:point withEvent:event]) { 
        NSLog(@"in view A"); 
        return self; 
    } 
    else { 
        return nil; 
    } 
 

深入
我們來更深入一下,現(xiàn)在有個實(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綁定的方法。處理完后消息就停止,整個過程結(jié)束。)

分析:

實(shí)現(xiàn)的方式多種,這里將兩個需求拆解開來實(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作為消息處理對象返回。

代碼如下:

復(fù)制代碼 代碼如下:

#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上時,則touch消息就被攔截在ViewA上,ViewB就收不到了。然后ButtonA就收到touch消息,會觸發(fā)onClick方法。

需求2的實(shí)現(xiàn),上面說到響應(yīng)鏈,ViewB只要override掉touches系列的方法,然后在自己處理完后,將消息傳遞給下一個響應(yīng)者(即父View即ViewA)。

代碼如下:在ViewB代碼里

復(fù)制代碼 代碼如下:

#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上寫:
復(fù)制代碼 代碼如下:

#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上寫:

復(fù)制代碼 代碼如下:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    // 本View不響應(yīng)用戶事件
    return NO;
 
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 伊吾县| 崇文区| 襄汾县| 盈江县| 清远市| 南部县| 门头沟区| 黄平县| 宜宾县| 盘锦市| 达孜县| 易门县| 开封县| 临江市| 灵寿县| 平阴县| 车致| 金乡县| 定州市| 印江| 阳西县| 新津县| 永州市| 文登市| 乾安县| 长顺县| 社旗县| 建昌县| 拉萨市| 长治县| 西昌市| 桐乡市| 美姑县| 和田市| 宁海县| 仁寿县| 平阳县| 开鲁县| 樟树市| 江津市| 饶平县|