ios - 是否有可能找到 iPhone 上某个点存在的所有 subview ?

标签 ios objective-c uiview tap

我希望能够在点击时检索有关放置在该点击点的所有不同 subview 的信息。例如,如果有一个背景,并且它前面是一个游戏角色,我希望能够点击该角色,并且它将检索表明该角色和背景都在该点击点的信息.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //retrieve info about what other subviews lie behind at this point
}

最佳答案

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {
   UITouch *touch=[touches anyObject];
   CGPoint p=[touch locationInView:self.view];
   NSLog(@"Coordiantes of Tap Point:(%f,%f)", p.x, p.y);

   NSArray *anArrayOfSubviews = [self.view subviews];
   NSMutableArray *requiredSubviewArray = [NSMutableArray array];

   for (UIView *aSubView in anArrayOfSubviews){
     if(CGRectContainsPoint(aSubView.frame, p)) {
        //aSubView is there at point 'p'
        [requiredSubviewArray addObject:aSubView];
     }
  }
// requiredSubviewArray contains all the Subviews at the Tap Point.

}

关于ios - 是否有可能找到 iPhone 上某个点存在的所有 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18756418/

相关文章:

ios - 在 Realm 中写入对象后调用完成 block

objective-c - 如何在 iOS 上的 Core Data 中设置新创建的关系?

iphone - 获取某个CGPoint下的 View 列表

ios - 重新排序 UIView subview

ios - 如何将 dispatch_data_t 转换为 NSData?

ios - 在需要时从 View Controller 显示/隐藏标签栏

iphone - UIScrollView 和 CATiledLayer 从头开始

iOS : assign a class to inner view using storyboard

ios - 在 iOS SDK 中逐行读取文件

ios - 设置一个类 IBOutlet 引用另一类文本字段的导出?