多个物体开始接触?

发布于 2024-10-16 17:48:12 字数 568 浏览 3 评论 0原文

我试图在 TouchsBegan 方法(2 UIImageViews)中拥有多个对象,

我正在使用下面的代码,但不起作用。没有错误,但位置混乱。我该怎么办?

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [[event allTouches] anyObject];

    if (image1.image == [UIImage imageNamed:@"ball.png"]){
         CGPoint location = [touch locationInView:touch.view];
         image1.center = location;
    }
    if (image1.image == [UIImage imageNamed:@"ball2.png"]){
         CGPoint location = [touch locationInView:touch.view];
         image2.center = location;
    }
}

I am trying to have multiple objects in a touchesbegan method (2 UIImageViews)

I am using the below code but isn't working. No errors but the location is just messed up. What should I do instead?

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [[event allTouches] anyObject];

    if (image1.image == [UIImage imageNamed:@"ball.png"]){
         CGPoint location = [touch locationInView:touch.view];
         image1.center = location;
    }
    if (image1.image == [UIImage imageNamed:@"ball2.png"]){
         CGPoint location = [touch locationInView:touch.view];
         image2.center = location;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是有多爱你 2024-10-23 17:48:12

如果您想识别 Touchsbegen 中的两个图像视图,请尝试此

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event //here enable the touch       
 {
// get touch event


UITouch *touch = [[event allTouches] anyObject];

CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(image1.frame, touchLocation))
{
    NSLog(@"image1 touched");
    //Your logic
}
if (CGRectContainsPoint(image2.frame, touchLocation))
{
    NSLog(@"image2 touched");
            //your logic
}
     }

希望有帮助

If you want to identify both image view in touchesbegen try this

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event //here enable the touch       
 {
// get touch event


UITouch *touch = [[event allTouches] anyObject];

CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(image1.frame, touchLocation))
{
    NSLog(@"image1 touched");
    //Your logic
}
if (CGRectContainsPoint(image2.frame, touchLocation))
{
    NSLog(@"image2 touched");
            //your logic
}
     }

Hope this help

静待花开 2024-10-23 17:48:12

我猜,如果您需要重叠 image1,第二个 if 条件中的 image1.image 需要 image2.image > 和 image2 中心。但如果您需要移动图像,则必须执行以下操作 -

  1. 检查触摸点是否属于两个对象。
  2. 如果是,则将两个图像相对移动(即将移动量添加到之前的图像中心)。没有将触摸点作为图像中心位置。

例如:如果image1中心位于(x1,y1)并且image2中心位于(x2,y2)。现在触摸点 (x3, y3) 同时属于图像 image1image2。如果新拖动的位置为 (x4,y4),则沿 x,y 方向的拖动量分别为 x4-x3y4-y3 。将此拖动量添加到图像的中心,以使图像出现在新位置。


伪代码

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    float touchXBeginPoint = [touch locationInView:touch.view].x;
    float touchYBeginPoint = [touch locationInView:touch.view].y;

    // Now Check touchXBeginPoint, touchYBeginPoint lies in image1, image2
    // Calculate the offset distance.

    if( /* both are true */ )
    {

       // Add the offset amount to the image1, image2 center.

    }

    else if( /* image1 touch is true */ )
    {
         // Add the offset amount to the image1 center
    }

    else if( /* image2 touch is true */ )
    {
         // Add the offset amount to the image2 center
    }
}

下载iPhone Game Dev第3章的源代码并查看图像如何移动。

I guess, image1.image in your second if condition needs to image2.image if you need to over lap image1 and image2 center. But if you need to move the images you have to do the follow -

  1. Check whether the touch point belongs to both the objects.
  2. If yes, move both the images relatively.( i.e., add the moved amount to the earlier image center ). Not making the touch point as the image center location.

Ex: If image1 center is at (x1, y1) and image2 center is at (x2, y2). Now the touch point (x3, y3) belongs both to image image1 and image2. If the new dragged is at (x4,y4), the drag amount is x4-x3 and y4-y3 along x,y directions respectively. Add this drag amount to both the image's center for the image to appear at the new location.


Pseudo Code

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    float touchXBeginPoint = [touch locationInView:touch.view].x;
    float touchYBeginPoint = [touch locationInView:touch.view].y;

    // Now Check touchXBeginPoint, touchYBeginPoint lies in image1, image2
    // Calculate the offset distance.

    if( /* both are true */ )
    {

       // Add the offset amount to the image1, image2 center.

    }

    else if( /* image1 touch is true */ )
    {
         // Add the offset amount to the image1 center
    }

    else if( /* image2 touch is true */ )
    {
         // Add the offset amount to the image2 center
    }
}

Download the source code of iPhone Game Dev chapter 3 and see how images are being moved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文