【问题标题】:Multiple objects in touchesbegan?touchesbegin 中的多个对象?
【发布时间】:2011-02-10 04:37:07
【问题描述】:

我试图在 touchesbegan 方法中包含多个对象 (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;
    }
}

【问题讨论】:

    标签: iphone touches


    【解决方案1】:

    如果你想在 touchesbegen 中识别两个图像视图,试试这个

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

    希望有帮助

    【讨论】:

    • 嘿,您的代码运行良好!但它不适用于多点触控。我需要能够同时拖动两个图像。这是我的代码: - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self.view];下面的第 2 部分...
    • if (CGRectContainsPoint(image1.frame, touchLocation)) { NSLog(@"image1 touch"); //你的逻辑CGPoint location = [touch locationInView:touch.view]; image1.center = 位置; } if (CGRectContainsPoint(image2.frame, touchLocation)) { NSLog(@"image2 touch"); //你的逻辑 CGPoint location = [touch locationInView:touch.view]; image2.center = 位置; } }
    【解决方案2】:

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

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

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


    伪代码

    - (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章的源代码,看看图像是如何移动的。

    【讨论】:

    • 我刚改了,不想重叠。我希望每个 UIImageView 都有自己的位置,并且能够同时拖动。
    • 我真的很困惑。接触点总是不等于两个图像,所以我想消除它。这就是我的距离: - (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 = 位置; } if (image2.image == [UIImage imageNamed:@"ball2.png"]){ CGPoint location = [touch locationInView:touch.view]; image2.center = 位置; } }
    猜你喜欢
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    相关资源
    最近更新 更多