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

我很抱歉,如果这看起来很模糊,但我不知道该怎么说。

我正在尝试构build一个ipad应用程序,让用户使用他们需要的工具填充工作区。 我需要一个让用户将组件拖放到应用程序界面的例子。 所以例如,如果我有一个应用程序,用户可以使一个窗体,我希望用户能够拖动文本框,列表,单选button等forms,我将如何去做呢? 我正在使用的项目将在popup式菜单中,以便用户拖动,然后将看到所有项目,我需要在该popup窗口内。 我只是不知道如何让他们从popup窗口拖动到canvas,也能够重用相同的项目(所以在例子中有3或4个文本框)。 这可能吗? 任何人都可以指导我在这个教程? 我已经search,但无法find任何东西。

如果这是错误的或是一个愚蠢的任务,请让我知道如何去做,这是我第一次在这里发表。

谢谢

已经有很多解释这个问题的答案。 基本上你可以制作自定义的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) {}]; } 

类似的问题

iPhone应用程序:在UIView中拖放图像的实现

在iOS中的基本拖放

iPhone拖放

在这里输入图像说明

从问题我你要build立一个function,如添加到购物车在大部分的购物应用程序,如亚马逊,翻转卡丁车等。

 - (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)]; // } } 

您可以使用这些代码来实现这些function。 使用这些代码你可以拖动对象在屏幕上的任何地方。

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