我有几个 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)];
}
}
任何人都可以帮忙吗?