Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding drag and drop component to iOS app

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.

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

like image 723
Andrew Avatar asked Jul 16 '13 14:07

Andrew


1 Answers

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:

-(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

Basic Drag and Drop in iOS

iPhone drag/drop

like image 125
Vojtech Vrbka Avatar answered Oct 13 '22 01:10

Vojtech Vrbka