2013-01-15 58 views
0

有人可以幫助我如何從給定的X,Y座標檢索iOS UI元素屬性嗎?UI元素座標到UI元素屬性

謝謝。

+0

' - [UIView hitTest:withEvent:]' - [docs here](http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/UIView/UIView.html#// apple_ref/DOC/UID/TP40006816-CH3-BBCCAICB)。 – 2013-01-15 18:31:44

+0

谷歌「命中測試」開始。沒有顯示更多的努力,你不會得到很多答案。 –

回答

1

一般而言,您需要- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 消息UIView。如果您的x, y在窗口座標中,則可以使用窗口的視圖:

UIView * view = [myWindow.rootViewController.view hitTest:CGPointMake(x,y)withEvent:nil];

這會使view成爲該位置視圖層次結構中最遠的元素。你需要確保你的座標位於元素的空間中。

如果您懷疑元素,您可能還喜歡- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

+0

感謝您的快速響應。 myWindow是應用程序「[[UIApplication sharedApplication] keyWindow]」的當前查看窗口?如果是的話,我沒有看到它的實例成員「查看」! – Amir

+0

Woops - 對不起。您需要首先向窗口詢問其視圖控制器,然後向視圖控制器請求視圖。 –