I Want to add drag & drop functionality on UIButton
. Actually I have created numbers of buttons programmatically under UIScrollView
. I add following target on UIButton
[mybutton addTarget:self action:@selector(wasDragged:withEvent:)forControlEvents:UIControlEventTouchDragInside];
and this is the method
- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
// get the touch
UIButton *myButton=[[UIButton alloc]init];
myButton=button;
[self.scrollView addSubview:myButton];
UITouch *touch = [[event touchesForView:button] anyObject];
//get delta
CGPoint previousLocation = [touch previousLocationInView:myButton];
CGPoint location = [touch locationInView:myButton];
CGFloat delta_x = location.x - previousLocation.x;
CGFloat delta_y = location.y - previousLocation.y;
//move button
myButton.center = CGPointMake(myButton.center.x + delta_x,myButton.center.y + delta_y);
}
This code is working fine under the scroll view and button is drag & drop inside the scrollview. Now problem is when I drag the button outside the scrollview, button disappear.
I want to drag button outside the scrollview also. How can I do this?
How can I drag button from Scrollview to another UIView? How is it possible?
ViewController.h
@interface ViewController : UIViewController <UIScrollViewDelegate>{
}
@property (nonatomic, strong) IBOutlet UIView *dropTarget;
@property (nonatomic, strong) UIImageView *dragObject;
@property (nonatomic, retain) UITextView *txt_View;
@property (nonatomic, strong) UIImageView *prevdragObject;
@property (nonatomic, assign) CGPoint touchOffset;
@property (nonatomic, assign) CGPoint homePosition;
@end
Just instead of
IBOutlet UIView *dropTarget
just use
IBOutlet UIScrollView *dropTarget
ViewController.m
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] == 1) {
// one finger
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
for (UIImageView *iView in self.view.subviews) {
if ([iView isMemberOfClass:[UIImageView class]]) {
if (touchPoint.x > iView.frame.origin.x &&
touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
touchPoint.y > iView.frame.origin.y &&
touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
{
self.dragObject = iView;
self.touchOffset = CGPointMake(touchPoint.x - iView.frame.origin.x,
touchPoint.y - iView.frame.origin.y);
self.homePosition = CGPointMake(iView.frame.origin.x,
iView.frame.origin.y);
[self.view bringSubviewToFront:self.dragObject];
}
}
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x,
touchPoint.y - touchOffset.y,
self.dragObject.frame.size.width,
self.dragObject.frame.size.height);
self.dragObject.frame = newDragObjectFrame;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
if (touchPoint.x > self.dropTarget.frame.origin.x &&
touchPoint.x < self.dropTarget.frame.origin.x + self.dropTarget.frame.size.width &&
touchPoint.y > self.dropTarget.frame.origin.y &&
touchPoint.y < self.dropTarget.frame.origin.y + self.dropTarget.frame.size.height )
{
self.dropTarget.backgroundColor = [UIColor colorWithPatternImage:self.dragObject.image];
}
self.dragObject.frame = CGRectMake(self.homePosition.x, self.homePosition.y, self.dragObject.frame.size.width, self.dragObject.frame.size.height);
self.prevdragObject.frame = self.dragObject.frame;
}
I solved this problem by changing code
[self.scrollView addSubview:myButton];
to
[self.view addSubview:myButton];
You need to move your dragging logic up to the view that contains the scroll view and the other view. When the user starts the drag on the higher view, you determine which subview is below it. Then reassign that view (the button) to be a subview of this higher view instead of its original parent view (the scroll view). Depending on where the drag is completed, the button is added back to the scroll view or the other view is was dragged to.