Reputation: 111
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?
Upvotes: 0
Views: 1966
Reputation: 119031
You're talking about 2 different things:
recognizer locationInView
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...
Upvotes: 1