Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何移动或拖动多个UIImage?_Ios_Uiimageview_Touches - Fatal编程技术网

Ios 如何移动或拖动多个UIImage?

Ios 如何移动或拖动多个UIImage?,ios,uiimageview,touches,Ios,Uiimageview,Touches,我有几个UIImage,我希望用户能够单独拖动每个UIImage。我希望他们能够触摸图像(必须触摸图像而不是其他地方),并在屏幕上拖动图像,而不会影响其他图像。此代码将两个图像同时移动到同一位置,无论用户在屏幕上触摸到什么位置: -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint location =

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

-(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)];
}
}

有人能帮忙吗?

试着把这个放在你的电脑里

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

您可以在下面使用。我用它在屏幕上移动多个图像。这对我有用

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];
移动(Pan):


这仍然只是拍摄两幅图像,并将它们移动到我在屏幕上单击的确切位置。
- (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];

}