Ios 如何移动或拖动多个UIImage?
我有几个UIImage,我希望用户能够单独拖动每个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 =
-(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];
}