Rasto
Rasto

Reputation: 17744

Use custom gesture recognizer to trigger scrolling of UIScrollView

I cannot find a way to replace the default UIPanGestureRecognizer of theUIScrollView. However I need to adjust the gesture that causes the UIScrollView to scroll.

I only want to recognize pans with higher velocity. In addition, the velocity of a finger is to be measured after it has moved certain distance (to be sure it is not the initial slow motion). To my knowledge this cannot be achieved with UScrollView's default UIPanGestureRecognizer.

How can I replace it with my own recognizer?

Upvotes: 4

Views: 3242

Answers (3)

clarkatron
clarkatron

Reputation: 69

You could implement the touchesMoved method in here as well. You could do a comparison between touches to check velocity. something like this but it is pseudo code

global firstpoint;
global secondpoint

override func touchesBegan(touches: NSSet, withEvent event: UIEvent){

for touch: AnyObject in touches {
        let firstpoint = (touch as UITouch).locationInNode(self);

}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {

    for touch: AnyObject in touches {
        let secondpoint = (touch as UITouch).locationInNode(self);
    }
    let diffx = secondpoint - firstpoint;
    if diffx > "some value you choose for velocity"{
         "do some action that you want";
    }
    else {
        firstpoint = secondpoint;
    }
}   

This will give you some velocity for the movement that you could use to supply a further action.

Upvotes: 1

A. R. Younce
A. R. Younce

Reputation: 1923

Changing the underlying gesture recognizer is the wrong way to go. What you want to do can probably be accomplished by implementing the gestureRecognizer(_:shouldReceiveTouch:) method of UIGestureRecognizerDelegate.

So long as the scroll view you want to customize isn't a part of a UICollectionView, UITableView, or a similar class then you'll be able to set the delegate of the gesture recognizer. Within the delegate you can then use the velocityInView(_:) method of UIPanGestureRecognizer to decide if you want to allow the gesture recognizer to trigger scrolling.

Upvotes: 1

Aron C
Aron C

Reputation: 838

You could try placing a clear UIView over the scrollView, and could then add a UIPanGestureRecognizer to it. You would then be able to set a selector for that GestureRecognizer that could then tell the scrollView to scroll. But then you will be responsible for determining the distance to scroll for each high velocity pan.

Upvotes: 0

Related Questions