0

我有 6 个 uiimageviews,比如 img1 - img6。当我触摸并拖动 img1 时,它会移动。但是当我拖动 img1 并且当它靠近其他 uiimageviews 时,img1 停止移动,靠近它的 img 开始移动。当我非常快速地拖动图像而不是当我缓慢拖动图像时会发生这种情况。而且拖动也不是那么顺利...... :(

这是我到目前为止所做的......

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint([self.firstImg frame], [touch locationInView:nil]))
{
    [self.view bringSubviewToFront:self.firstImg];
    self.firstImg.center = [touch locationInView:nil];
}
else if (CGRectContainsPoint([self.secondImg frame], [touch locationInView:nil]))
{
    [self.view bringSubviewToFront:self.secondImg];
    self.secondImg.center = [touch locationInView:nil];
}
else if (CGRectContainsPoint([self.thirdImg frame], [touch locationInView:nil]))
{
    [self.view bringSubviewToFront:self.thirdImg];
    self.thirdImg.center = [touch locationInView:nil];
}
else if (CGRectContainsPoint([self.fourthImg frame], [touch locationInView:nil]))
{
    [self.view bringSubviewToFront:self.fourthImg];
    self.fourthImg.center = [touch locationInView:nil];
}
else if (CGRectContainsPoint([self.fifthImg frame], [touch locationInView:nil]))
{          
    [self.view bringSubviewToFront:self.fifthImg];
    self.fifthImg.center = [touch locationInView:nil];
}
else if (CGRectContainsPoint([self.sixthImg frame], [touch locationInView:nil]))
{        
    [self.view bringSubviewToFront:self.sixthImg];
    self.sixthImg.center = [touch locationInView:nil];
}
}
4

1 回答 1

2

您的实施存在许多问题:

  • 您将nil作为视图传递给locationInView:,这意味着如果您移动超级视图或支持界面旋转,您将获得不正确的坐标。

  • 您正在将图像视图的中心设置为触摸位置。正因为如此,当用户第一次触摸一个视图时,如果触摸没有完全位于视图的中心,视图将跳转到触摸位置的中心。这不是用户期望的行为。

  • 您总是以固定的顺序检查视图,而不是从前到后检查视图。这就是为什么“当它靠近其他 uiimageview 时,img1 停止移动,而靠近它的 img 开始移动。” firstImg如果触摸结束,您的代码将始终移动firstImg,即使用户正在拖动secondImg,因为您总是在检查firstImg之前检查secondImg

  • 你经常重复自己。如果你必须写两次相同的东西,你应该考虑把它分解成一个单独的函数或方法。如果你必须写三遍(或更多)相同的东西,你几乎肯定应该把它排除在外。

所有这些问题最简单的答案就是停止使用touchesMoved:withEvent:和相关的方法。相反,UIPanGestureRecognizer为每个图像视图添加一个:

- (void)makeImageViewDraggable:(UIImageView *)imageView {
    imageView.userInteractionEnabled = YES;
    UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewPannerDidFire:)];
    [imageView addGestureRecognizer:panner];
}

- (void)imageViewPannerDidFire:(UIPanGestureRecognizer *)panner {
    UIView *view = panner.view;
    [view.superview bringSubviewToFront:view];

    CGPoint translation = [panner locationInView:view];
    CGPoint center = view.center;
    center.x += translation.x;
    center.y += translation.y;
    view.center = center;

    [panner setTranslation:CGPointZero inView:view];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeImageViewDraggable:self.firstImg];
    [self makeImageViewDraggable:self.secondImg];
    [self makeImageViewDraggable:self.thirdImg];
    [self makeImageViewDraggable:self.fourthImg];
    [self makeImageViewDraggable:self.fifthImg];
    [self makeImageViewDraggable:self.sixthImg];
}
于 2013-04-13T07:00:42.033 回答