#pragma?mark?在UIView視圖結構中查找點擊view的觸摸事件 -?(UIView?*)hitTest:(CGPoint)point?withEvent:(UIEvent?*)event{ ???? ????//?如果不可以交互?|?隱藏?|?透明度太小?3者任意一個都忽略不能點擊 ????if(!self.userInteractionEnabled?||?self.hidden?||?self.alpha<=0.01){ ????????return?nil; ????} ????if([self?pointInside:point?withEvent:event]){ ???????? ???????__block?UIView?*hit?=??nil; ???????? ????????//NSEnumerationReverse?倒序 ????????[self.subviews?enumerateObjectsWithOptions:(NSEnumerationReverse)?usingBlock:^(__kindof?UIView?*?_Nonnull?obj,?NSUInteger?idx,?BOOL?*?_Nonnull?stop)?{ ???????????? ????????????///進行坐標轉化 ????????????CGPoint?vonverPoint?=?[self?convertPoint:point?toView:obj]; ????????????hit?=?[obj?hitTest:vonverPoint?withEvent:event]; ????????????if(hit){ ????????????????*stop?=?YES; ????????????} ????????}]; ????????if(hit){ ????????????return?hit; ????????}else{ ????????????return?self; ????????} ????}else{ ????????return?nil; ????} } #pragma?mark?觸摸的位置是否在當前view視圖中合適的點擊位置 -?(BOOL)pointInside:(CGPoint)point?withEvent:(UIEvent?*)event{ ???? ????CGFloat?x1?=?point.x;?//?觸摸的x ????CGFloat?y1?=?point.y;?//?觸摸的y ???? ????CGFloat?x2?=?self.frame.size.width/2;?//view的?x ????CGFloat?y2?=?self.frame.size.height/2;?//view的?y ???? ???? ????double?dis?=?sqrt((x1-x2?)*(x1-x2)?+?(y1-y2?)*(y1-y2));?//這里設置自己的判斷,button的大小?或者按鈕中心的一點?或者buton里放置的最大圓等 ????if(dis<=self.frame.size.width/2){?//判斷當前觸摸的點是不是自己要點擊的區(qū)域 ????????return?YES; ????}else{ ????????return?NO; ????} }
在UIView中做的轉盤游戲,我們需要判斷點擊的位置,然后做對應的點擊事件處理,我們就可以用上面的辦法,或者tabBar凸出的部分等都會用到,首先就要了解hitTest是什么,然后他的調用順序才能合理的應用,來極大的縮短時間。
成都創(chuàng)新互聯(lián)是專業(yè)的印江網(wǎng)站建設公司,印江接單;提供成都網(wǎng)站建設、成都網(wǎng)站制作,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行印江網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!