1

I'm trying to use UIPanGestureRecognizer to flip an UIView. I'm using below code to handle flipping the view,

- (void)handlePan:(UIPanGestureRecognizer*)gesture{

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

    NSLog(@"PAN X VALUE %f", translation.x );

    double percentageOfWidth = translation.x / (gesture.view.frame.size.width / 2);

    float angle = (percentageOfWidth * 100) * M_PI_2 / 180.0f;

    CALayer *layer = testView.layer;
    CATransform3D flipTransform = CATransform3DIdentity;
    flipTransform.m34 = -0.002f;

    flipTransform = CATransform3DRotate(flipTransform, angle, 0.0f, 1.0f, 0.0f);
    layer.transform = flipTransform;

}

My problem is when i pan, sometimes there are some quick jumpings happen, I believe thats because translation.x(PAN X VALUE) value jumps from few points ahead, In my case i need it to be very smooth.

Any help greatly appreciated.

Thanks in advance.

Prasad De Zoysa
  • 2,488
  • 2
  • 23
  • 30
  • Some similar questions may answer you: http://stackoverflow.com/questions/9740821/uipangesturerecognizer-not-moving-object-smoothly http://stackoverflow.com/questions/14264804/uipangesturerecognizer-move-view-instantly – Joiningss Nov 14 '13 at 08:28
  • Above answer doesn't helped me. Thanks for the link though. – Prasad De Zoysa Nov 14 '13 at 10:18

2 Answers2

0

You can use the gestureRecognizerShouldBegin method, which u can limit the UIPanGestureRecognizer sensitivity.

Example:

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer {
    CGPoint translation = [panGestureRecognizer translationInView:self.view];
    return fabs(translation.y) < fabs(translation.x);
}
Mutawe
  • 6,464
  • 3
  • 47
  • 90
  • Thanks for the response @Mutawe, but this doesn't solve my problem, Check `PAN X VALUE` log when you swipe your finger over the screen. It jumps from few values. For example 1.0 to 10.0, and then again 10.4, 10.8, etc... – Prasad De Zoysa Nov 14 '13 at 09:40
0

Here's how I solved this for a cube rotation - just take the amount dragged and divide:

- (void)panHandle:(UIPanGestureRecognizer*)recognizer;
{
    if ([recognizer state] == UIGestureRecognizerStateBegan)
    {
        CGPoint translation = [recognizer translationInView:[self superview]];
        _startingX = translation.x;
    }
    else if ([recognizer state] == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [recognizer translationInView:[self superview]];
        CGFloat angle = -(_startingX - translation.x) / 4;
        //Now do translation with angle
        _transitionContainer.layer.sublayerTransform = [self->_currentMetrics asTransformWithAngle:angle];
    }
    else if ([recognizer state] == UIGestureRecognizerStateEnded)
    {
        [self transitionOrReturnToOrigin];
    }
}
Jasper Blues
  • 28,258
  • 22
  • 102
  • 185