Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPanGestureRecognizer translationInView returning different values on iPhone/ipad

Hi I have this bit of code in my project,

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    CGPoint translation = [scrollView.panGestureRecognizer translationInView:scrollView];
    if(translation.x < 0.0f) {
        // Something
    }
}  

and it works fine on iPhone, but for some reason on iPad, my CGPoint is always returning (0,0). Any ideas as to why?

like image 337
K.He Avatar asked Oct 25 '25 20:10

K.He


1 Answers

I had the same problem, and came up with a nasty workaround, using the velocityInView() method instead. I don't have a real iPad, so I'm suspicious that the problem may actually be with the simulator.

Objective-C:

CGPoint velocity = [scrollView.panGestureRecognizer velocityInView: scrollView];
CGPoint translation = CGPointMake(velocity.x * 0.1, velocity.y * 0.1);

Swift:

let translation = scrollView.panGestureRecognizer.velocityInView(self) * 0.1

I'm using a very handy CGPoint extension you can find right here.

like image 98
Elliot Fiske Avatar answered Oct 29 '25 17:10

Elliot Fiske



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!