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 に答える