无法使用touchesMoved同时拖动2个imageView

时间:2011-10-14 12:53:01

标签: iphone objective-c ios xcode

我正在进行一场乒乓球比赛,当我作为2名球员进行比赛时,图像不会同时移动:我必须释放一张图片以移动另一个。

以下是代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesMoved:touches withEvent:event];
}


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

if (location.x > 240) {
    CGPoint ylocation = CGPointMake(player.center.x, location.y);
    player.center = ylocation;

}
if (mode == kdual) {
if (location.x < 240) {
    CGPoint ylocation = CGPointMake(cpu.center.x, location.y);
    cpu.center = ylocation;
    }
}       
}

1 个答案:

答案 0 :(得分:1)

您只能检查一个触摸物体的位置。您应该检查所有触摸对象,并相应地移动图像。像这样:

for (UITouch * touch in [touches allObjects])
{
     // Check position of touch and move the images
     CGPoint location = [touch locationInView:touch.view];

     if (location.x > 240) {
           CGPoint ylocation = CGPointMake(player.center.x, location.y);
           player.center = ylocation;

     }
     if (mode == kdual) {
           if (location.x < 240) {
           CGPoint ylocation = CGPointMake(cpu.center.x, location.y);
           cpu.center = ylocation;
           }
     } 
}