2

我希望能够在点击时检索有关放置在该点击位置的所有不同子视图的信息。例如,如果有一个背景,并且它前面是一个游戏角色,我希望能够点击那个角色,它会检索到信息说角色和背景都在那个点击位置.

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

4 回答 4

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.

}

于 2013-09-12T06:27:08.390 回答
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().
于 2013-09-12T06:26:21.180 回答
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
}
于 2013-09-12T06:21:49.137 回答
-1

你可以试试这个。。

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

 -(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 
        }
    }
于 2013-09-12T06:26:42.793 回答