【问题标题】:How can you move or drag multiple UIImages?如何移动或拖动多个 UIImage?
【发布时间】:2013-09-25 23:40:39
【问题描述】:

我有几个 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)];
}
}

谁能帮忙?

【问题讨论】:

    标签: ios uiimageview touches


    【解决方案1】:

    试着把它放在你的

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

    【讨论】:

    • 这仍然只是拍摄两张图像并将它们移动到我在屏幕上单击的任何位置。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-01-13
      • 2021-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      相关资源
      最近更新 更多