iOS | 怎么獲取view在父類中的frame, 或子控件位置轉(zhuǎn)換成父控件位置

怎么獲取view在父類中的frame, 或者說 父類UIView中SubView的坐標(biāo)怎么轉(zhuǎn)換成在父類UIView中的坐標(biāo)

// 將像素point由point所在視圖轉(zhuǎn)換到目標(biāo)視圖view中,返回在目標(biāo)視圖view中的像素值

- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;

// 將像素point從view中轉(zhuǎn)換到當(dāng)前視圖中,返回在當(dāng)前視圖中的像素值

- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;

// 將rect由rect所在視圖轉(zhuǎn)換到目標(biāo)視圖view中,返回在目標(biāo)視圖view中的rect

- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;

// 將rect從view中轉(zhuǎn)換到當(dāng)前視圖中,返回在當(dāng)前視圖中的rect

- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;

例把UITableViewCell中的subview(btn)的frame轉(zhuǎn)換到 controllerA中

// controllerA 中有一個(gè)UITableView, UITableView里有多行UITableVieCell,cell上放有一個(gè)button

// 在controllerA中實(shí)現(xiàn):

CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];

CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];

// 此rc為btn在controllerA中的rect

或當(dāng)已知btn時(shí):

CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];

CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];



子控件位置轉(zhuǎn)換成父控件的位置

CGRect focusFrame = [_scrollView convertRect:_joinView.frametoView:self.view];

這里_scrollView是self.view的子控件

_joinView是_scrollView的子控件,這里是計(jì)算出_joinView在self.view的位置,

當(dāng)然還有其他類似的方法

[view convertPoint:<#(CGPoint)#> fromView:<#(UIView *)#>]

[view convertPoint:<#(CGPoint)#> toView:<#(UIView *)#>]

[self.view convertRect:<#(CGRect)#> fromView:<#(UIView *)#>]

[self.view convertRect:<#(CGRect)#> toView:<#(UIView *)#>]


2.同一坐標(biāo)系的比較

判斷 rect1是否包含 rect2,返回bool

BOOL isContain = CGRectContainsRect(rect1, rect2);

判斷 rect1和 rect2是否有重疊,返回bool

BOOL isIntersect = CGRectIntersectsRect(rect1, rect2);

判斷點(diǎn)point是否在rect中,返回bool

BOOL isContain = CGRectContainsPoint(rect, point);

實(shí)例1:判斷UIView控件是否在主窗口上

方法寫在UIView的Category中

- (BOOL)isShowingOnKeyWindow {

//主窗口

UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

//以主窗口左上角為坐標(biāo)原點(diǎn),計(jì)算self的矩形框

CGRect newFrame = [keyWindow convertRect:self.bounds fromView:self];

CGRect winBounds = keyWindow.bounds;

//主窗口的bounds 和 self的矩形框 是否有重疊

BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);

return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;

}

實(shí)例2:判斷兩個(gè)View是否有重疊

方法寫在UIView的Category中

- (BOOL)intersectWithView:(UIView *)view {

//主窗口

UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

//以主窗口左上角為坐標(biāo)原點(diǎn),計(jì)算self和view的矩形框

CGRect selfRect = [self convertRect:self.bounds toView:keyWindow];

CGRect viewRect = [view convertRect:view.bounds toView:keyWindow];

//返回是否有重疊的結(jié)果

return CGRectIntersectsRect(selfRect, viewRect);

}

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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