拥有多个具有不同触摸的移动 UIImage 视图

发布于 2024-12-06 01:58:39 字数 142 浏览 0 评论 0原文

我正在努力尝试让多个 UIImageView 在拖动时移动。我最初将所有运动都放在 TouchesBegan 下。但是,当我拖动单个对象时,它们都会消失,只有我拖动的对象会移动。

如何让每个图像都拖动自己的术语(我拖动第一个图像,只有第一个图像移动等)?

I am working on trying to get multiple UIImageViews to move when they are dragged. I was originally putting all the movement under TouchesBegan. However, when I drag a single object, they all disappear and only the one I am dragging moves.

How can I have each image dragged own its own terms(I drag the first image, only the first image moves etc.)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼睛会笑 2024-12-13 01:58:39

我不确定这是否是您正在寻找的内容,但我不久前就从事过此工作,所以希望它有用。

只需确保在 XIB 文件中设置了 7 个 UIImageView,链接它们就可以了。

@synthesize img1, img2, img3, img4, img5, img6, magnet;

-(void) checkInsertion {
    if (CGRectContainsRect(img2.frame, img1.frame)) {img1.center = img2.center;}
    if (CGRectContainsRect(img4.frame, img3.frame)) {img3.center = img4.center;}
    if (CGRectContainsRect(img6.frame, img5.frame)) {img5.center = img6.center;}

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    if (CGRectIntersectsRect(magnet.frame, img1.frame)) {img1.center = magnet.center;}
    if (CGRectIntersectsRect(magnet.frame, img2.frame)) {img2.center = magnet.center;}
    if (CGRectIntersectsRect(magnet.frame, img3.frame)) {img3.center = magnet.center;}
    if (CGRectIntersectsRect(magnet.frame, img4.frame)) {img4.center = magnet.center;}
    if (CGRectIntersectsRect(magnet.frame, img5.frame)) {img5.center = magnet.center;}
    if (CGRectIntersectsRect(magnet.frame, img6.frame)) {img6.center = magnet.center;}
    [UIView commitAnimations];

}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [[event allTouches] anyObject];
    CGPoint touchLoc = [touch locationInView:touch.view];

    if (CGRectContainsPoint(img1.frame, touchLoc)) {img1.center = touchLoc;}
    if (CGRectContainsPoint(img3.frame, touchLoc)) {img3.center = touchLoc;}
    if (CGRectContainsPoint(img5.frame, touchLoc)) {img5.center = touchLoc;}

    if (CGRectContainsPoint(magnet.frame, touchLoc)) {magnet.center = touchLoc;}

    [self checkInsertion];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesBegan:touches withEvent:event];
}

I'm not sure if this is what you're looking for but I worked on this a while ago, so hopefully it's useful.

Just make sure you set up 7 UIImageViews in the XIB File, link them and you'll be good to go.

@synthesize img1, img2, img3, img4, img5, img6, magnet;

-(void) checkInsertion {
    if (CGRectContainsRect(img2.frame, img1.frame)) {img1.center = img2.center;}
    if (CGRectContainsRect(img4.frame, img3.frame)) {img3.center = img4.center;}
    if (CGRectContainsRect(img6.frame, img5.frame)) {img5.center = img6.center;}

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    if (CGRectIntersectsRect(magnet.frame, img1.frame)) {img1.center = magnet.center;}
    if (CGRectIntersectsRect(magnet.frame, img2.frame)) {img2.center = magnet.center;}
    if (CGRectIntersectsRect(magnet.frame, img3.frame)) {img3.center = magnet.center;}
    if (CGRectIntersectsRect(magnet.frame, img4.frame)) {img4.center = magnet.center;}
    if (CGRectIntersectsRect(magnet.frame, img5.frame)) {img5.center = magnet.center;}
    if (CGRectIntersectsRect(magnet.frame, img6.frame)) {img6.center = magnet.center;}
    [UIView commitAnimations];

}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [[event allTouches] anyObject];
    CGPoint touchLoc = [touch locationInView:touch.view];

    if (CGRectContainsPoint(img1.frame, touchLoc)) {img1.center = touchLoc;}
    if (CGRectContainsPoint(img3.frame, touchLoc)) {img3.center = touchLoc;}
    if (CGRectContainsPoint(img5.frame, touchLoc)) {img5.center = touchLoc;}

    if (CGRectContainsPoint(magnet.frame, touchLoc)) {magnet.center = touchLoc;}

    [self checkInsertion];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesBegan:touches withEvent:event];
}
满栀 2024-12-13 01:58:39

您应该查看UIPanGestureRecognizer。在负责这些多个图像视图的视图控制器中,向每个图像视图添加平移手势识别器。视图控制器应该负责响应平移手势回调并相应地移动图像视图的框架。

值得注意的是,您还应该获取图像视图中触摸相对于中心的偏移量并将其存储。然后,当平移视图时,您需要将偏移量考虑到计算中。这样,如果用户开始拖动远离中心点的某个位置,视图不会跳到用户的手指上。

请参阅 2010 年和 2011 年有关 UIScrollView 的 WWDC 会议视频。特别是从 2011 年开始。他们举例说明了如何做到这一点。

You should look at UIPanGestureRecognizer. In the view controller that's responsible for these multiple image views, add a pan gesture recognizer to each of the image views. The view controller should be responsible for responding to the pan gesture callbacks and move the frame of the image view accordingly.

Of note, you should also grab the offset of the touch within the image view from the center and store this. Then, when the view is panned, you need to factor in the offset into your calculations. This is so the view doesn't just jump to the user's finger if they start dragging somewhere distant from the center point.

See the WWDC session videos from 2010 and 2011 about UIScrollView. Especially from 2011. They go into examples of how to do some of this.

鱼窥荷 2024-12-13 01:58:39

我已经让它工作了!

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event touchesForView:self.view] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    if(CGRectContainsPoint(plusone.frame, location))
    {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        plusone.center = location;
    }
    if(CGRectContainsPoint(plustwo.frame, location))
    {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        plustwo.center = location;
    }
    if(CGRectContainsPoint(plusthree.frame, location))
    {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        plusthree.center = location;
    }
    if(CGRectContainsPoint(minusone.frame, location))
    {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        minusone.center = location;
    }
    if(CGRectContainsPoint(minustwo.frame, location))
    {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        minustwo.center = location;
    }
    if(CGRectContainsPoint(minusthree.frame, location))
    {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        minusthree.center = location;
    }

但是,当一个图像与另一个图像碰撞时,它就消失了!
有人能解决这个问题吗?

I have gotten it to work!

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event touchesForView:self.view] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    if(CGRectContainsPoint(plusone.frame, location))
    {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        plusone.center = location;
    }
    if(CGRectContainsPoint(plustwo.frame, location))
    {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        plustwo.center = location;
    }
    if(CGRectContainsPoint(plusthree.frame, location))
    {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        plusthree.center = location;
    }
    if(CGRectContainsPoint(minusone.frame, location))
    {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        minusone.center = location;
    }
    if(CGRectContainsPoint(minustwo.frame, location))
    {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        minustwo.center = location;
    }
    if(CGRectContainsPoint(minusthree.frame, location))
    {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        minusthree.center = location;
    }

But, When An Image Collides With Another, It Disappears!
Does anyone have a fix to this problem?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文