Question

Please refer to the image below for the following question: The blue and orange circles you see are UIView's. I would like to be able to drag the blue UIView which has a UIPanGestureRecognizer and have the orange circle translate accordingly and stay the same distance from the blue circle as it does now. Meaning, if I drag the blue circle down, the orange UIView should stay parallel with the blue circle and translate down as well. If I drag the blue circle to the right, the orange circle should translate to the right as well and stay the same distance from the blue circle as it does now.

Image

So right now the circles are part of the same class that have a UIPanGestureRecognizer on them. Here is the code to drag these views:

-(void)dragging:(UIPanGestureRecognizer *)p {


UIView *newView = p.view;
if (p.state == UIGestureRecognizerStateBegan) {
    self.origC = newView.center;
}
self.delta = [p translationInView:newView.superview];
CGPoint c = self.origC;

c.x +=self.delta.x;
c.y +=self.delta.y;

newView.center = c;

[self.delegate refreshView];
}

Within the UIPanGestureRecognizer class, I am able to obtain the translation of the pan gesture in the coordinate system of the specified view with:

self.delta = [p translationInView:newView.superview];

I think that this is the delta I need to apply for the translation of the orange circle, but I'm not sure how I would go about doing that? Any help would be appreciated. Thank you!

Was it helpful?

Solution

Do it exactly as you are doing it for the directly dragged point.

if (p.state == UIGestureRecognizerStateBegan) {
    self.origC = newView.center; // your code
    self.origD = otherView.center; // new code
}
CGPoint c = self.origC; // your code
c.x +=self.delta.x; // your code
c.y +=self.delta.y; // your code
newView.center = c; // your code
CGPoint d = self.origD; // new code
d.x +=self.delta.x; // new code
d.y +=self.delta.y; // new code
otherView.center = d; // new code

OTHER TIPS

To my understanding, the pasted code works however you want to find "a one line code" way,right ? I guess what you seeking something like this.

orangeView.transform = CGAffineTransformMakeTranslation(delta.x,delta.y); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top