Reputation: 6471
I have UIScrollView contain many UIImageViews. I need to drag an UIImage from UIScrollView to another view. But my code drags only in UIScrollView.I can't drag an image to outside the scrollView . Please help me.. My code is..
for (int i = 0; i < [myArray count]; i++) {
image = [[UIImageView alloc]initWithFrame:CGRectMake(0, y, 75, 30)];
image.userInteractionEnabled = YES;
image.backgroundColor = [UIColor blueColor];
UIPanGestureRecognizer *panRecg = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(labelDragged:)];
[image addGestureRecognizer:panRecg];
y=y+35;
[gScrollView addSubview:image];
}
-(void)labelDragged:(UIPanGestureRecognizer *)recognizer {
UIImageView *imageView = (UIImageView *)recognizer.view;
CGPoint newCenter = [recognizer translationInView:self.view];
if([recognizer state] == UIGestureRecognizerStateBegan) {
beginX = imageView.center.x;
beginY = imageView.center.y;
}
newCenter = CGPointMake(beginX + newCenter.x, beginY + newCenter.y);
[imageView setCenter:newCenter];
}
Upvotes: 1
Views: 1319
Reputation: 2330
Here is way to drag
-(void)labelDragged:(UIPanGestureRecognizer *)recognizer {
{
UIImageView *imageView=(UIButton *)[recognizer view];
CGPoint newCenter = [recognizer translationInView:self.view];
if (recognizer.state==UIGestureRecognizerStateBegan) {
CGRect rect=[self.view convertRect:[imageView frame] fromView:[imageView superview]];
beginX = imageView.center.x;
beginY = imageView.center.y;
[vew setFrame:rect];
[self.view addSubview:vew];
}
else
{
CGRect rect=imageView.frame;
newCenter.x=(newCenter.x+Begin.x);
newCenter.y=(newCenter.y+Begin.y);
imageView.center=newCenter;
if (CGRectIntersectsRect(rect, [AcceptingView frame])) {
[AcceptingView setBackgroundColor:[UIColor lightGrayColor]];//Notifying that the AcceptingView will accept the icon
}
else
{
//change back to old color
}
}
if (recognizer.state==UIGestureRecognizerStateEnded)
{
CGRect rect=[imageView frame];
if (CGRectIntersectsRect(rect, [AcceptingView frame])) {
//your method of adding the Image to acceptView
CGRect rect=[acceptView bounds];
rect.size=imageView.frame.size;
imageView.frame=rect;
[AcceptingView addSubView:imageView];
}
else//else part is means for if user drop the dragged view somewhere else other than AcceptingView
{
//add image back to scrollView
}
}
}
Upvotes: 1
Reputation: 437482
A couple of observations:
When dragging it off the scrollview, you want to remove it from the scroll view and add it to, for example, self.view
. You'll then do addSubview
and adjust the coordinates accordingly:
newCenter = [self.view convertPoint:newCenter fromView:imageView.superview];
[self.view addSubview:imageView];
imageView.center = newCenter;
Make sure to set clipsToBounds
for the scroll view to NO, so you can continue to see it as you drag it off.
Note, I've changed this to refer to imageView.superview
rather than the scroll view. If you keep scrollview references in there, you have to add conditional checks to make sure that the image view hasn't already been moved off the scroll view.
Upvotes: 2
Reputation: 62686
I have a couple UI questions for you in comments, but @Rob is correct that the key to giving the appearance of moving the view around the hierarchy is to actually move it within the hierarchy. When dragging begins...
CGRect newFrame = [gScrollView convertRect:imageView.frame toView:self.view];
imageView.frame = newFrame;
[self.view addSubview:imageView];
The gesture recognizer should remain intact.
Upvotes: 1