2013-07-16 53 views
11

如果這看起來很模糊,但我不確定該怎麼說它,我很抱歉。將拖放組件添加到iOS應用程序

我想構建一個ipad應用程序,讓用戶用他們需要的工具填充工作區。我需要一個讓用戶將組件拖放到應用程序界面的示例。因此,例如,如果我有一個應用程序,用戶可以製作一個表單,並且我希望用戶能夠拖動文本框,列表,單選按鈕等來創建表單,那麼我該怎麼做呢?我正在使用的項目將出現在彈出式菜單中,以便用戶拖動並隨後會在彈出窗口中看到我需要的所有項目。我只是不知道如何讓他們從彈出窗口拖動到畫布,也能夠重用相同的項目(所以在例子中有3或4個文本框)。這可能嗎?任何人都可以指導我到這個教程?我已經搜索,但無法找到任何東西。

如果這是錯誤的或是一個愚蠢的任務請讓我知道如何做到這一點,這是我第一次在這裏發佈。

謝謝

+0

您是否實現了這個目標?如果您有任何問題,請分享源代碼。提前致謝。 – lazyCoder

回答

8

已經有很多解釋這個問題的答案。 基本上你可以製作自定義的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 App: implementation of Drag and drop images in UIView

Basic Drag and Drop in iOS

iPhone drag/drop

0

enter image description here

從這個問題我的事情你要建立一個類似的功能添加到購物車中的大多數購物應用程序,如亞馬遜,翻轉和卡丁車等..

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


    // } 
} 

,您可以使用這些代碼來實現這些feauture。使用這些代碼可以將對象拖動到屏幕上的任何位置。您可以使用鏈接。

相關問題