iPhone App:在 UIView 中拖放图片的实现

Posted

技术标签:

【中文标题】iPhone App:在 UIView 中拖放图片的实现【英文标题】:iPhone App: implementation of Drag and drop images in UIView 【发布时间】:2012-01-29 07:06:29 【问题描述】:

在我的 iPhone 应用程序中,我想实现如下所示的功能

用户可以从一个视图中选择一个形状(图像)并放入另一个视图

我怎样才能做到这一点?

【问题讨论】:

http://***.com/questions/17678735/adding-drag-and-drop-component-to-ios-app/31963306#31963306 你可以试试这个链接享受编码:) 【参考方案1】:

最简单的方法是使用 UIPanGestureRecognizer。这将在视图被移动和被删除时给你消息。当它被移动时,更新它的中心。当它被放下时,检查它的位置是否在您想要放入的视图的范围内。(您可能需要将坐标转换为目标视图的坐标系。)如果是,请执行适当的操作。

【讨论】:

【参考方案2】:

试试下面的链接drag and drop image around the screen

还有try this

【讨论】:

嗨,您的第一个链接已失效。以供将来参考,您能否在评论中发布链接的内容。 SO 旨在成为一站式商店,不应链接到潜在的死链接 链接 1 缓存:web.archive.org/web/20081022074350/www.icodeblog.com/2008/10/20/…【参考方案3】:

你可以参考之前的帖子,肯定会帮助你实现这个功能...

    Basic Drag and Drop in iOS Building drag and drop interface on iphone iPhone drag/drop

对于以上所有链接,您必须记住,您需要首先为任何控件获取TouchesBegin 事件,然后您必须为同一控件获取TouchesMoved 事件。

TouchesMoved 事件中,您只需要获取控件的中心点(CGPoint)即可。当您释放控件时,该控件将设置为CGPoint。如果这会产生问题,那么您可以将 CGPoint 放入变量中,并在 TouchesEnded 事件中设置该点。

对于您的情况,我认为您必须维护视图的层次结构...否则在拖动视图时可能不可见...

更多编码部分:

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

        NSLog(@"%f,%f", self.center.x, self.center.y);
        CGPoint newLoc = CGPointZero;

        newLoc = [self.mainView convertPoint:[[touches anyObject] locationInView:self.superview] toView:self.superview];
        float newX = newLoc.x + self.superview.frame.origin.x + (self.frame.size.width /2) + [[touches anyObject] locationInView:self].x ;
        float newY = newLoc.y - (((UIScrollView *)self.superview).contentOffset.y *2) ;
        NSLog(@"content offset %f", ((UIScrollView *)self.superview).contentOffset.y);

        self.scrollParent.scrollEnabled = NO;
        NSLog(@"%f,%f", self.center.x, self.center.y);

        newLoc = CGPointMake(newX, newY);
        [self.superview touchesCancelled:touches withEvent:event];                                                               
        [self removeFromSuperview];
        NSLog(@"%f,%f", self.center.x, self.center.y);


        self.center = CGPointMake(newLoc.x, newLoc.y);
        [self.mainView addSubview:self];
        NSLog(@"%f,%f", self.center.x, self.center.y);

        [self.mainView bringSubviewToFront:self];
        isInScrollview = NO;
   

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 

    [UIView beginAnimations:@"stalk" context:nil];
    [UIView setAnimationDuration:.001];
    [UIView setAnimationBeginsFromCurrentState:YES];

    UITouch *touch = [touches anyObject];

    self.center = [touch locationInView: self.superview];

    [UIView commitAnimations];
    if ((self.center.x + (self.frame.size.width / 2)) > 150 && hasExitedDrawer && !self.scrollParent.dragging ) 
        self.scrollParent.scrollEnabled = NO;

        [self.delegate moveItemsDownFromIndex: ((self.center.y + (self.scrollParent.contentOffset.y)) / 44) + 1 ];
        //NSLog(@"%i", ((self.center.y + (self.scrollParent.contentOffset.y *2)) / 44) + 1);
    

    if (self.center.x + (self.frame.size.width / 2) < 150) 

        hasExitedDrawer = YES;

    

   

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 

    if ((self.center.x + (self.frame.size.width / 2)) > 150 && hasExitedDrawer && !self.scrollParent.dragging ) 
        CGPoint newLoc = CGPointZero;

        newLoc = [self.scrollParent convertPoint:[[touches anyObject] locationInView:self.superview] toView:self.superview];
        float newY = newLoc.y + (self.scrollParent.contentOffset.y *2);

        [self.scrollParent insertSubview:self atIndex:((self.center.y + (self.scrollParent.contentOffset.y)) / 44) ];
        self.frame = CGRectMake(0, newY, self.frame.size.width, self.frame.size.height);
        isInScrollview = YES;
        hasExitedDrawer = NO;
    

此代码可能包含一些无关紧要的内容,但可以让您了解更多...

【讨论】:

【参考方案4】:

您可以使用以下开源库:

    Library 1 OBDragDrop Library 2 AJWDraggableDroppable Library 3 iOS-DragAndDrop Library 4 ios-drag-and-drop

【讨论】:

【参考方案5】:

Adding drag and drop component to iOS app问题

您将要在大多数购物应用程序(如 Amazon、Flip kart 等)中构建一个功能,例如添加到购物车。

【讨论】:

【参考方案6】:

三步解决方案

1) 你必须实现/监听触摸事件

2) 实现拖动和触摸事件

3) 然后管理 movetosuperview 方法。

【讨论】:

如何通过对齐拖放到区域***.com/a/9233102/1537178的源代码

以上是关于iPhone App:在 UIView 中拖放图片的实现的主要内容,如果未能解决你的问题,请参考以下文章

如何将 UIView(例如 UIImageView)拖放到 iPhone/iPad 表格中的单元格

在 MFC 对话框中拖放

在MFC对话框中拖放

在android中拖放textview

Java在列表中拖放图像

在 NSView 中拖放