문제

With the Pan GestureComponent, I am trying to get the first location of the current object dragged. For that, I did:

Firstable, I store the position of the button when State Began.

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer{

    CGPoint startlocation;

// Get Position X and Y
if (recognizer.state == UIGestureRecognizerStateBegan) {
    startlocation = [recognizer locationInView:self.view];
    NSLog(@"%f" @"-" @"%f", startlocation.x , startlocation.y);
}

And when the user release the boutton (State Ended), I want to set the button back at the first place.

// Quand on lache le composant : Action de fin
if (recognizer.state == UIGestureRecognizerStateEnded)
{     
NSLog(@"%f" @"-" @"%f", startlocation.x , startlocation.y); 
recognizer.view.center = startLocation;  
}

NSLog:

State Began:

76.000000-158.000000

State endend:

0.000000--1.998666

I have a problem. My button is outSide my screen. I don't know why X and Y of startLocation are modified?

도움이 되었습니까?

해결책

You're talking about 2 different things:

  1. The position of the touch that is triggering the pan gesture recognizer locationInView
  2. The position of the view (you don't say if you're using the frame origin or the center)

It isn't clear what you're trying to do but you need to decide, are you moving the target view by the pan gesture translation:

if (recognizer.state == UIGestureRecognizerStateBegan) {
   startLocation = targetView.center;
}

CGPoint translation = [recognizer translationInView:self.view];

CGPoint newCenter = startLocation;
newCenter.x += translation.x;
newCenter.y += translation.y;

targetView.center = newCenter;

or you're trying to snap the target view to the pan gesture location

targetView.center = [recognizer locationInView:self.view];

This is all assuming that the target view is a direct subview of self.view...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top