1

I try to implement the pan gesture programmatically but when I try it, the uiview always move back to the origin place inside the superview during the move. So in fact it jumps from the origin to the new location back an forth. Does anyone know what is the problem?

Code:

- (void)setGesture
{
    self.userInteractionEnabled = YES;
        UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(detectPan:)];
        self.gestureRecognizers = @[panRecognizer];


}

- (void) detectPan:(UIPanGestureRecognizer *) penGes
{
    if ((penGes.state == UIGestureRecognizerStateChanged) ||
        (penGes.state == UIGestureRecognizerStateEnded))
    {

        CGPoint translation = [penGes translationInView:self.superview];
        self.center = CGPointMake(self.lastLocation.x + translation.x,
                              self.lastLocation.y + translation.y);

       [penGes setTranslation:CGPointZero inView:self];
    }
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Promote the touched view
    [self.superview bringSubviewToFront:self];

    // Remember original location
    self.lastLocation = self.center;
}
Avi Levin
  • 1,868
  • 23
  • 32

1 Answers1

0

I think that your translation is conflicting with Autolayout constraints.
Take a look at this answer from matt about transformations and Autolayout.

Community
  • 1
  • 1
Quentin Hayot
  • 7,786
  • 6
  • 45
  • 62