2013-07-16 13 views
11

पर ड्रैग और ड्रॉप घटक जोड़ना मैं क्षमा चाहता हूं अगर यह बहुत अस्पष्ट लगता है लेकिन मुझे यकीन नहीं है कि यह कैसे और शब्द है।आईओएस ऐप

मैं उन उपकरणों वे जरूरत के साथ एक कार्यक्षेत्र को पॉप्युलेट करने देगा कि एक 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 लागू करने के लिए कर सकते हैं जा रहे हैं। इन कोड का उपयोग करके आप ऑब्जेक्ट को स्क्रीन पर कहीं भी खींच सकते हैं। नमूना परियोजना के लिए

आप this लिंक का उपयोग कर सकते हैं।