[英]determine if point on screen is within specific UIScrollView subview
UIScrollView包含幾個UIView對象; 如何判斷屏幕上沒有觸摸生成的點是否在滾動視圖的特定子視圖內? 到目前為止,嘗試確定點是否在子視圖中總是返回父滾動視圖的子視圖數組中的第一個子視圖,即坐標是相對於滾動視圖而不是窗口。
這是我嘗試過的(編輯過的)
-(UIView *)viewVisibleInScrollView
{
CGPoint point = CGPointMake(512, 384);
for (UIView *myView in theScrollView.subviews)
{
if(CGRectContainsPoint([myView frame], point))
{
NSLog(@"In View");
return myView;
}
}
return nil;
}
看起來你的點是相對於窗口的,並且你想要它相對於當前視圖。 convertPoint:fromView:
應該有幫助。
這里可能有錯誤,但看起來應該是這樣的:
-(UIView *)viewVisibleInScrollView
{
CGPoint point = CGPointMake(512, 384);
CGPoint relativePoint = [theScrollView convertPoint:point fromView:nil]; // Using nil converts from the window coordinates.
for (UIView *myView in theScrollView.subviews)
{
if(CGRectContainsPoint([myView frame], relativePoint))
{
NSLog(@"In View");
return myView;
}
}
return nil;
}
這是我做的:
@implementation UIScrollView (FOO)
- (id)foo_subviewAtPoint:(CGPoint)point
{
point.x += self.contentOffset.x;
for (UIView *subview in self.subviews) {
if (CGRectContainsPoint(subview.frame, point)) {
return subview;
}
}
return nil;
}
@end
以下是我如何使用它:
CGPoint center = [scrollView convertPoint:scrollView.center fromView:scrollView.superview];
UIView *view = [scrollView foo_subviewAtPoint:center];
有些事情需要注意:
foo_subviewAtPoint:
的點在滾動視圖的坐標系中表示 。 這就是為什么在上面的代碼中我必須將center
轉換為其超視圖的坐標系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.