0

我有几个 UIImages,我希望用户能够单独拖动每一个。我希望他们能够触摸图像(必须触摸图像而不是其他地方),并将其拖过屏幕而不影响其他图像。无论用户触摸屏幕的哪个位置,此代码都会同时将两个图像移动到同一个位置:

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

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

我尝试使用这样的 if 语句,它完全停止了操作,根本没有拖动:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if (touch.view == playerCardOne) {
    playerCardOne.center = location;
    [self.view bringSubviewToFront:(playerCardOne)];
}
else if ([touch view] ==playerCardTwo) {
playerCardTwo.center = location;
[self.view bringSubviewToFront:(playerCardTwo)];
}
}

任何人都可以帮忙吗?

4

2 回答 2

0

试着把它放在你的

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

   if ([touch view] == playerCardOne) 
   {
       playerCardOne.center = location;
   }
   else if ([touch view] == playerCardTwo) 
   {
       playerCardTwo.center = location;
   }
}

并取出:

[self touchesBegan:touches withEvent:event];

并确保设置userInteractionEnabled = YES;

于 2013-09-26T00:58:59.083 回答
0

你可以在下面使用。我将它用于在屏幕上移动的多个图像。它对我有用。

UIPanGestureRecognizer *span=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onsPan:)];

 [smyImageView addGestureRecognizer:span];
smyImageView.userInteractionEnabled = YES;


   UIPanGestureRecognizer *span1=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onsPan1:)];

   [smyImageView1 addGestureRecognizer:span1];

移动(平移):

- (void)onsPan:(UIPanGestureRecognizer *)recognizer {

   CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}

- (void)onsPan1:(UIPanGestureRecognizer *)recognizer {

    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}
于 2013-12-17T15:23:39.217 回答