iOS 座標轉換convertPoint

2022-07-04 17:27:15 字數 2050 閱讀 7635

在開發的過程中,不免會遇到子控制項超過父檢視的情況。都知道,超出父檢視的部分是不能響應點選事件,但是總有些情況需要我們讓超出的部分響應點選事件,那麼convertpoint就可以大顯身手了。小白可參考,大神請指點。

先說解決方法

在父檢視重寫下面的方法

//重寫該方法後可以讓超出父檢視範圍的子檢視響應事件

- (uiview *)hittest:(cgpoint)point withevent:(uievent *)event }}

return view;

}

為什麼上面的方法能解決問題呢

這和ios的事件分發機制 hit-testing有關,簡單的說,hit-testing的作用就是找出你每次觸控螢幕,點到的究竟是哪個view。

如圖:

參考圖當使用者去點選view-c的時候,hit-testing實際上是這樣檢測的:uiview中其實提供了兩個方法來確定hit-testview:

- (uiview *)hittest:(cgpoint)point withevent:(uievent *)event;

- (bool)pointinside:(cgpoint)point withevent:(uievent *)event;

注意:其實在每次遞迴去呼叫hittest:(cgpoint)point withevent:(uievent *)event之前,都會呼叫pointinside:withevent:來確定該觸控點是否在該view內。

// 將畫素point由point所在檢視轉換到目標檢視view中,返回在目標檢視view中的畫素值

- (cgpoint)convertpoint:(cgpoint)point toview:(uiview *)view;

// 將畫素point從view中轉換到當前檢視中,返回在當前檢視中的畫素值

- (cgpoint)convertpoint:(cgpoint)point fromview:(uiview *)view;

// 將rect由rect所在檢視轉換到目標檢視view中,返回在目標檢視view中的rect

- (cgrect)convertrect:(cgrect)rect toview:(uiview *)view;

// 將rect從view中轉換到當前檢視中,返回在當前檢視中的rect

- (cgrect)convertrect:(cgrect)rect fromview:(uiview *)view;

例把uitableviewcell中的subview(btn)的frame轉換到 controllera中

// controllera 中有乙個uitableview, uitableview裡有多行uitableviecell,cell上放有乙個button

// 在controllera中實現:

cgrect rc = [cell convertrect:cell.btn.frame toview:self.view];

或者cgrect rc = [self.view convertrect:cell.btn.frame fromview:cell];

// 此rc為btn在controllera中的rect

當已知btn時:

cgrect rc = [btn.superview convertrect:btn.frame toview:self.view];

或者cgrect rc = [self.view convertrect:btn.frame fromview:btn.superview];

感謝 -- zhanglizhi111

iOS座標轉換

座標系的作用是為了便於描述點的位置。話說mac,ios中的各種座標系總會讓初學者摸不著頭腦,一會兒這樣一會兒那樣。不過有一點是不變的,z軸的正方向總是指向觀察者,也就是垂直螢幕平面向上。1.nsview座標系 在mac中nsview的座標系預設是右手座標系 view其實是二維座標系,但是為了方便我們...

ios座標系轉換

1.cgrect convertrect cgrect rect toview uiview view 文件解釋 converts a rectangle from the receiver s coordinate system to that of another view.意思就是說把乙個矩形...

iOS座標系轉換

把乙個矩形從接收者的座標系轉換到另乙個檢視 view 的座標系中.注 若view引數為空,則轉換為視窗 window 的座標系 接收者與view都必須是同一視窗 window 的物件。下面是把選中的from單元的frame轉換為相對於當前視窗座標系的座標 預設情況下frame是以父控制項 windo...