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)