事件

事件包括有三類(lèi):Touch Motion Remote 本篇主要介紹touch事件

Touch事件

事件產(chǎn)生->事件分發(fā)->事件響應(yīng)

每產(chǎn)生一個(gè)時(shí)間都會(huì)產(chǎn)生一個(gè)UIEvent對(duì)象,該對(duì)象記錄了事件、類(lèi)型、觸點(diǎn)等信息

@interface UIEvent:NSObject

屬性有 type,subType

方法:-(nullable NSSet<UITouch*>*)allTouches//說(shuō)明UIEvent持有UITouch,也能用以判斷是幾個(gè)點(diǎn)的觸控

@interface UITouch:NSObject

屬性 phase,tapCount,gestureRecgnizers

UIView中有方法-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event//用以判斷當(dāng)前的touch是點(diǎn)在了那個(gè)View上

hitTest方法從UIWindow開(kāi)始父類(lèi)傳到子類(lèi),subViews按照逆順序遍歷

事件分發(fā)從UIAppliction一直到hitTestView

-(void)sendEvent:(UIEvent*)event;

touch響應(yīng)事件?

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

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent*)event

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent*)event

Responder Chain從子類(lèi)到父類(lèi)傳遞 一直到AppDelegate如果都沒(méi)有響應(yīng)就丟棄


實(shí)例 加大按鈕的點(diǎn)擊區(qū)域

方法1

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

? ??//使用cgrectInset作用是 改變目標(biāo)frame的inset

? ? CGRect targetRect=CGRectInset(_button.frame, -ExtraPadding, -ExtraPadding);

? ? //重點(diǎn) 查看點(diǎn)擊區(qū)域在targetRect中嗎

? ? if(CGRectContainsPoint(targetRect,point)){

? ? ? ? //把這一區(qū)域設(shè)置為button的點(diǎn)擊區(qū)域

? ? ? ? return?_button;

? ? }

? ? else

? ? ? ? return?[super?hitTest:pointwithEvent:event];

}

方法二: 調(diào)用hitTest會(huì)先調(diào)用ponitInside

創(chuàng)建button類(lèi) 重寫(xiě)

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

? ? CGRecttargetRect=CGRectInset(self.bounds,-20, -20);

? ? //如果想使用frame 因?yàn)閒rame是父view的坐標(biāo)系 所以需要轉(zhuǎn)化

? ? //CGPoint convertPoint=[self convertPoint:point toView:self.superview];

? ? //CGRect targetRectFrame=CGRectInset(self.frame, -20, -20);

? ? //此時(shí)下方的point應(yīng)該使用convertPoint

? ? if(CGRectContainsPoint(targetRect, point)){

? ? ? ? return??YES;

? ? }

? ? else

? ? return? [super?pointInside:pointwithEvent:event];

子View超過(guò)了父View的范圍

方法修改父View的pointInside

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

{

? ? //ponit應(yīng)該是button的坐標(biāo)系中 而此時(shí)point是view的 因此需要轉(zhuǎn)化?

? ? if([_button pointInside:[self convertPoint:point toView:_button] withEvent:event])

? ? {

? ? ? ? return?YES;

? ? }

? ? else

? ? ? return? [super?pointInside:pointwithEvent:event];

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • iOS觸摸事件處理 主要是記錄下iOS的界面觸摸事件處理機(jī)制,然后用一個(gè)實(shí)例來(lái)說(shuō)明下應(yīng)用場(chǎng)景.一、處理機(jī)制界面響應(yīng)...
    醉葉惜秋閱讀 603評(píng)論 0 0
  • 前言: 按照時(shí)間順序,事件的生命周期是這樣的: 事件的產(chǎn)生和傳遞(事件如何從父控件傳遞到子控件并尋找到最合適的vi...
    reviewThis閱讀 813評(píng)論 1 2
  • 早上八點(diǎn)二十五分的航班,我六點(diǎn)半鐘就拉著巨大的行李箱出現(xiàn)在北京機(jī)場(chǎng)2號(hào)航站樓,等待其他團(tuán)友的到來(lái)。七點(diǎn)不到,此次墨...
    funnyeyeandy閱讀 452評(píng)論 0 0
  • 經(jīng)過(guò)周末的休整,又開(kāi)始了忙碌而充實(shí)的工作。 昨晚就到了市里,清早起床趕往工作地,日程安排得滿(mǎn)滿(mǎn)的,直到剛才才忙完。...
    安福閱讀 173評(píng)論 0 1
  • 周末布置給學(xué)生們的作業(yè),完成的質(zhì)量還都不錯(cuò),讓我著實(shí)感到欣慰。課堂上,我依舊采取提問(wèn)的方式跟學(xué)生們一起復(fù)習(xí)。但是這...
    H晶晶姑娘閱讀 264評(píng)論 0 0

友情鏈接更多精彩內(nèi)容