我有几个UIImages,我希望用户能够单独拖动每个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)];
}
}
有人可以帮忙吗?
答案 0 :(得分: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;
答案 1 :(得分: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];
}