简体   繁体   中英

Drag image from one view to another view In iphone/ipad

I am facing below problem:- Please have a look in the below image the thing what i want is:- 在此输入图像描述

I want to drag default images from view 1 to view 2 and images have to be always thr in view1 so its not like touch moved to draging images . i tried a lot things in that but i succeed in draging image from one view to another but in view2 i am not able to get touch points so its just adding thr as frame But i cnt able to touch tht image across view2 and even in view2 i want to do other functionality like zooming and others but first want to get touch points in view2. I am giving description image about this problem.

The edited question is:-

i have done this simple demo in this i am transferring one view to another view and after getting view2 points its been in its limit boundaries. but how can i get the default things remain in their. i will modify this code i will add images in this view. but its just shows my thinking here so guide me here.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];   
    if ([touch view] == view3) {
        CGPoint location = [touch locationInView:self.view];
        view3.center = location;        
        return;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
        int tmpValue = 0;
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];          
        {
            onceDone = TRUE;
            if(onceDone == TRUE)
            {
                UITouch *touch = [touches anyObject];
                CGPoint pt = [touch locationInView:self.view];
                NSLog(@"X = %f y=%f",pt.x,pt.y);
                view3.center = pt;
                if(view3.frame.origin.x < view2.frame.origin.x )
                   //view3.frame.origin.x < view2.frame.origin.x)
                    [view3 setFrame:CGRectMake(159,view3.frame.origin.y, view3.frame.size.width, view3.frame.size.height)];

            }



        }
    [UIView commitAnimations];
    }

Please help me. it is urgent. Thanks in advance

如果您正在使用平移手势,则使用touchesMoved方法拖放图像。将view1和view2的超级视图设置为相同的UIView,并根据该公共超级视图计算您的手势位置或触摸点。这意味着您的view1和view2将是在同一视图上,每个子视图(图像)也只添加到主视图中。因此,您可以在主视图上随意移动图像,并可以对它们执行任何操作。

We have been working in a similar issue, and we have made up with this solution:

  1. We have an UIPanGestureRecognizer added to each button.

  2. When we detect the pan gesture begin, we create the object we want to drag (in this case, it could be a copy of your image). You should have to set a rect equals to the button, or if you create the object in a different view, you should have to convert the object coordinates so the object seems in the same position as the button, but inside his view.

  3. After that we move the last created object, instead of move the button.

And that is, the trick here is the UIPanGestureRecognizer works until you untouch the screen, but it is triggered while you move your finger for all the screen, not only inside the button!

I hope this works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM