UIScrollView motion recognition by pixel

I need to change the subviews of the UIScrollView according to their place on the screen so that they become smaller when moving up and larger when moving down.

Is there a way to find out contentOffset with each pixel changing? I catch the scrollViewDidScroll: method, but whenever the movement is fast, 200pxls can occur between two calls.

Any ideas?

+4
source share
1 answer

You have basically two approaches:

  • subclass UIScrollView and override touchesBegan/Moved/Ended ;

  • add your own UIPanGestureRecognizer to your current UIScrollView .

  • set a timer, and every time it fires, refresh the display of your view _scrollview.contentOffset.x ;

In the first case, you will use touch processing methods:

 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { UITouch* touch = [touches anyObject]; _initialLocation = [touch locationInView:self.view]; _initialTime = touch.timestamp; <more processing here> //-- this will make the touch be processed as if your own logics were not there [super touchesBegan:touches withEvent:event]; } 

I am sure you need to do this for touchesMoved ; I don’t know if you need something concrete when the gesture begins or ends; in this case also override touchesMoved: and touchesEnded: Also think about touchesCancelled:

In the second case, you would do something like:

 //-- add somewhere the gesture recognizer to the scroll view UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)]; panRecognizer.delegate = self; [scrollView addGestureRecognizer:panRecognizer]; //-- define this delegate method inside the same class to make both your gesture //-- recognizer and UIScrollView own work together - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return TRUE; } 

The third case is pretty trivial to implement. Not sure if this will give better results than the other two.

+3
source

All Articles