iOS:在 UIImageView 之间移动图像

标签 ios

我有一个 View Controller ,其中有一个默认的 UIview 放置在整个屏幕上。我在此 View 中添加了几个 UIImageView(大约 20 个)。我想支持在这些 UIImageView 之间交换图像。我已经使用 UITapGestureRecogniser 来处理这些 UIImageView 上发生的触摸。我可以在 UITapGestureRecogniser 事件本身下处理这些 UIImageView 之间的图像拖放吗? (或)我需要使用 UIPanGestureRecognizer 或触摸事件吗?实现这些UIImageView之间的图像拖放的最简单、最容易的方法是什么?请建议?

谢谢!

最佳答案

一些想法。

  1. 您可以通过点击手势来实现此目的(例如点击第一个 ImageView ,点击目标 ImageView ),但这不是很直观。对于最终用户来说,使用 UIPanGestureRecognizer 进行适当的拖放操作会更加直观。

  2. 从技术上讲,您并不是从 ImageView 中拖动图像,而是实际上拖动 ImageView 本身。 (如果没有 ImageView ,图像本身就没有视觉表示。)当您放手时,您将为 ImageView 帧的变化设置动画以完成幻觉。


如果您有一个 imageViews 的 NSArray,您可以向其 super View 添加手势:

@property (nonatomic, strong) NSMutableArray *imageViews;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self createImageViewArray];

    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                              action:@selector(handlePan:)];
    [self.view addGestureRecognizer:gesture];
}

- (void)createImageViewArray
{
    self.imageViews = [NSMutableArray array];

    // add your imageviews to the array any way you want

    ...
}

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    static UIImageView *draggedImage = nil;
    static CGRect draggedImageOriginalFrame;

    CGPoint location = [gesture locationInView:gesture.view];

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        // see if we grabbed an imageview

        draggedImage = [self determineImageForLocation:location];

        // if so, save its old location for future reference and bring it to the front

        if (draggedImage)
        {
            draggedImageOriginalFrame = draggedImage.frame;
            [draggedImage.superview bringSubviewToFront:draggedImage];
        }
    }
    else if (gesture.state == UIGestureRecognizerStateChanged && draggedImage != nil)
    {
        // if we're dragging it, then update its location 

        CGPoint translation = [gesture translationInView:gesture.view];
        CGRect frame = draggedImageOriginalFrame;
        frame.origin.x += translation.x;
        frame.origin.y += translation.y;
        draggedImage.frame = frame;
    }
    else if (draggedImage != nil && (gesture.state == UIGestureRecognizerStateEnded ||
                                     gesture.state == UIGestureRecognizerStateCancelled ||
                                     gesture.state == UIGestureRecognizerStateFailed))
    {
        // if we let go, let's see if we dropped it over another image view

        UIImageView *droppedOver = nil;

        if (gesture.state == UIGestureRecognizerStateEnded)
            droppedOver = [self draggedImageView:draggedImage toLocation:location];

        if (droppedOver == nil)
        {
            // fail; animate the restoring of the view back to it's original position

            [UIView animateWithDuration:0.25
                             animations:^{
                                 draggedImage.frame = draggedImageOriginalFrame;
                             }];
        }
        else
        {
            // succeed; make sure to bring the view we're about to animate to the front

            [droppedOver.superview bringSubviewToFront:droppedOver];

            // animate the swapping of the views

            [UIView animateWithDuration:0.25
                             animations:^{
                                 draggedImage.frame = droppedOver.frame;
                                 droppedOver.frame = draggedImageOriginalFrame;
                             }];
        }
    }
}

// see if we dragged an imageview over one of our imageviews, returning a point
// to the image view we dropped it over

- (UIImageView *)draggedImageView:(UIImageView *)draggedView toLocation:(CGPoint)location
{
    for (UIImageView *imageview in self.imageViews)
        if (CGRectContainsPoint(imageview.frame, location) && imageview != draggedView)
            return imageview;

    return nil;
}

// see if we started the gesture over an imageview
// (turns out that this is the same as "did we drag an image over another",
// but as we're not dragging an image yet, just pass nil)

- (UIImageView *)determineImageForLocation:(CGPoint)location
{
    return [self draggedImageView:nil toLocation:location];
}

关于iOS:在 UIImageView 之间移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14965910/

相关文章:

ios - 具有属性的 NSMutableArray

iphone - iOS 中文章搜索的搜索栏

ios - UIDocumentPickerViewController - 未调用委托(delegate)方法

ios - iOS 拼写检查器存储哪些信息?

ios - opencv2 IOS 框架链接器错误

ios - 将 View Controller View 添加到 subview 时发现 nil

ios - 从后台激活 iOS 应用程序的快速机制

iphone - 使用 openGL 和 cocos2D 绘制大量线条的最佳方法是什么?

ios - UITableViewCell 和 AutoLayout 打破约束的两种状态

ios 无法识别的选择器