Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Drag and Drop a View from one Position to Another

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 297
    Comment on it

    To add a Drag and Drop functionality in your Application You just have to append the below Code in your app.

     

     //This function is called when the user touches any object
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        
        UITouch* aTouch = [touches anyObject];
        UIView* aView = [aTouch view];//This will take out the view the user wants to drag
        if (aView != self.view) {
            [self.view bringSubviewToFront:aView];
            label.text = @"You're Dragging...";//This label is just to show that the user is Dragging an object
        }
    }
    //This Fuction is called when the user starts moving the View
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        
        UITouch* aTouch = [touches anyObject];
        UIView* aView = [aTouch view];
        if (aView != self.view) {
            aView.center = [aTouch locationInView:self.view];
        }
    }
    //This function is called when the user drops the View in a location
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        
        label.text = @"At Ease";
    }

     

    NOTE:You have to disable Autolayout feature from your application to make the above code working.

     

    You can Update the code according to the need in the project.

     

    Thanx for Reading

    Keep Coding..:)

    Garima

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: