是否有可能找到iphone上某个点存在的所有子视图?

时间:2013-09-12 05:58:52

标签: ios objective-c uiview tap

我希望能够在点按时检索信息,了解放置在该点按位置的所有不同子视图。例如,如果有背景,并且在它前面是游戏角色,我希望能够点击该角色,它将检索信息,表示角色和背景都在该点击点

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

4 个答案:

答案 0 :(得分:4)

-(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.

}

答案 1 :(得分:0)

试试这个。这对你有帮助..

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //retrieve info about what other subviews lie behind at this point
   NSLog(@"subviews--%@",[self.view subviews]); //it prints all subviews as Array
    NSLog(@"superview--%@",[self.view superview]); //it prints superview of your view
}

答案 2 :(得分:0)

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [[event allTouches] anyObject];  // here you get all subviews on ur touchpoint

if ([touch view] == imageView) {

    CGPoint location = [touch locationInView: self.view];

    imageView.center = location;

}

}

///  To check both views are in one spot, use this

// CGRectContainsRect() and CGRectIntersectsRect().

答案 3 :(得分:-1)

你可以试试这个......

如果您使用[self.view子视图],则可以获取所有子视图。您可以将这些视图框架与触摸位置进行比较。

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
       CGPoint touchLocation = [touch locationInView:self.view];
       startX = touchLocation.x;
       startY = touchLocation.y;

        for (UIView *view in [self.view subviews]){
           //Here you can compare each view frame with touch location 
        }
    }