将拖放组件添加到 iOS 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17678735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:20:43  来源:igfitidea点击:

Adding drag and drop component to iOS app

iosobjective-c

提问by Andrew

I apologise if this seems very vague but I'm not sure how else to word it.

如果这看起来很模糊,我深表歉意,但我不知道如何表达。

I am trying to build an ipad app that will let users populate a workspace with tools they need. I need an example of letting a user drag and drop components into the app interface. So for example if i had an app that the user can make a form and I want the user to be able to drag text boxes, lists, radio buttons etc to make the form, how would i go about doing that? The items that i am using will be in a popup menu so that the user drags up and then will see all the items that i need inside that popup. Im just not sure how to let them drag from the popup to the canvas and also be able to reuse the same item (so have 3 or 4 text boxes in the example). Is this possible? Can anyone direct me to a tutorial on this? I have searched but cant find anything.

我正在尝试构建一个 ipad 应用程序,让用户使用他们需要的工具填充工作区。我需要一个让用户将组件拖放到应用程序界面的示例。例如,如果我有一个用户可以制作表单的应用程序,并且我希望用户能够拖动文本框、列表、单选按钮等来制作表单,我将如何去做?我正在使用的项目将在弹出菜单中,以便用户向上拖动,然后将在该弹出菜单中看到我需要的所有项目。我只是不确定如何让它们从弹出窗口拖到画布上,并且还可以重用相同的项目(因此在示例中有 3 或 4 个文本框)。这可能吗?任何人都可以指导我阅读有关此的教程吗?我已经搜索过但找不到任何东西。

If this is laid out wrong or is a stupid quest please let me know how to do it, this is my first time posting on here.

如果这是布局错误或愚蠢的任务,请告诉我该怎么做,这是我第一次在这里发帖。

Thank you

谢谢

采纳答案by Vojtech Vrbka

There are already many answers explaining this question. Basically you can make custom UIView, which after touching will be following the touch movement. Something like this:

已经有很多答案解释了这个问题。基本上你可以制作自定义 UIView,它在触摸后会跟随触摸移动。像这样的东西:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    _originalPosition = self.view.center;
    _touchOffset = CGPointMake(self.view.center.x-position.x,self.view.center.y-position.y);
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{        
    UITouch *touch = [touches anyObject];
    CGPoint position = [touch locationInView: self.view.superview];

    [UIView animateWithDuration:.001
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^ {

                         self.view.center = CGPointMake(position.x+_touchOffset.x, position.y+_touchOffset.y);
                     }
                     completion:^(BOOL finished) {}];
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint positionInView = [touch locationInView:self.view];
    CGPoint newPosition;
    if (CGRectContainsPoint(_desiredView.frame, positionInView)) {
        newPosition = positionInView;
        // _desiredView is view where the user can drag the view
    } else {
        newPosition = _originalPosition;
        // its outside the desired view so lets move the view back to start position
    }

    [UIView animateWithDuration:0.4
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^ {
                         self.view.center = newPosition
                         // to 
                     }
                     completion:^(BOOL finished) {}];

}

Similar questions

类似问题

iPhone App: implementation of Drag and drop images in UIView

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

Basic Drag and Drop in iOS

iOS 中的基本拖放

iPhone drag/drop

iPhone 拖放

回答by SARATH SASI

enter image description here

在此处输入图片说明

From the Question i thing you are going to Build a feature like Add to cart in Most of the Shopping Apps like Amazon, Flip kart and etc..

从问题 i 中,您将在大多数购物应用程序(例如亚马逊、Flip Kart 等)中构建一个功能,例如添加到购物车。

- (void)viewDidLoad {
    [super viewDidLoad];

    shoeImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(46,222, 51 , 51)];
    shoeImageView1.image = [UIImage imageNamed:@"shoe.png"];

    shoeImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(150,222, 51 , 51)];
    shoeImageView2.image = [UIImage imageNamed:@"shoe1.png"];

    shoeImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(225,222, 51 , 51)];
    shoeImageView3.image = [UIImage imageNamed:@"shoe2.png"];


     addTOCart = [[UIImageView alloc]initWithFrame:CGRectMake(132,400, 80, 80)];
    addTOCart.image = [UIImage imageNamed:@"basket.png"];
    [self.view addSubview:addTOCart];

    imageViewArray = [[NSMutableArray alloc]initWithObjects: shoeImageView1,shoeImageView2 ,shoeImageView3,nil];



    for (int i=0; i<imageViewArray.count; i++) {

        [self.view addSubview:[imageViewArray objectAtIndex:i]];
        [[imageViewArray objectAtIndex:i]setUserInteractionEnabled:YES];

//[self touchesBegan:imageViewArray[i] withEvent:nil];


    }


}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for(int i=0 ; i< imageViewArray.count; i++)
{

    CGPoint pt = [[touches anyObject]locationInView:self.view];

    startLocation = pt;



    newtemp = [imageViewArray objectAtIndex:i];

    UITouch* bTouch = [touches anyObject];

    if ([bTouch.view isEqual:newtemp])
    {
        firstTouchPoint = [bTouch locationInView:[self view]];
        oldX = firstTouchPoint.x - [[bTouch view]center].x;
        oldY = firstTouchPoint.y - [[bTouch view]center].y;
    }

}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    for(int i=0 ; i< imageViewArray.count; i++)
    {
    newtemp = [imageViewArray objectAtIndex:i];

        //oldLoc = newtemp.frame;


    if (CGRectContainsPoint(addTOCart.frame , newtemp.frame.origin))
    {
        NSLog(@"touched");
               dragging = NO;

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Cart" message:@"Added to Cart" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [[imageViewArray objectAtIndex:i] setImage:[UIImage imageNamed:@""]];


        [imageViewArray removeObjectAtIndex:i];

        [alert show];


        break;

    }

    else
    {
        //[newtemp setCenter:CGPointMake(startLocation.x-oldX, startLocation.y-oldY)];
    }
       // self.view.userInteractionEnabled= NO;

    }


    dragging = NO;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch* mTouch = [touches anyObject];
   // if ([mTouch.view isEqual: newtemp]) {
        CGPoint cp = [mTouch locationInView:[self view]];
        [[mTouch view]setCenter:CGPointMake(cp.x-oldX, cp.y-oldY)];


   // }
}

you can use these codes to implement these feauture. Using these code u can drag the object any where in the screen.

您可以使用这些代码来实现这些功能。使用这些代码,您可以将对象拖动到屏幕中的任何位置。

for sample project you can use thisLink.

对于示例项目,您可以使用链接。