문제

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!

도움이 되었습니까?

해결책

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

다른 팁

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); 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top