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

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

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

我怎样才能做到这一点?

【问题讨论】:

标签: iphone objective-c ios4 drag-and-drop


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    还有try this

    【讨论】:

    【解决方案3】:

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

    1. Basic Drag and Drop in iOS
    2. Building drag and drop interface on iphone
    3. 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】:
      【解决方案5】:

      Adding drag and drop component to iOS app问题

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

      【讨论】:

        【解决方案6】:

        三步解决方案

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

        2) 实现拖动和触摸事件

        3) 然后管理 movetosuperview 方法。

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-31
        • 1970-01-01
        • 1970-01-01
        • 2012-02-06
        • 2011-08-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多