Use a custom gesture recognizer to trigger UIScrollView scrolling
I cannot find a way to replace the default UIPanGestureRecognizer
UIScrollView
. However, I need to customize a gesture that causes scrolling UIScrollView
.
I want to recognize pans faster. In addition, the finger speed should be measured after it has moved a certain distance (to make sure this is not an initial deceleration). As far as I know, this cannot be achieved with the UScrollView
default UIPanGestureRecognizer
.
How can I replace it with my own resolver?
source to share
Changing the main gesture recognizer is the wrong way. What you want to do can perhaps be achieved by implementing the gestureRecognizer (_: shouldReceiveTouch :) UIGestureRecognizerDelegate method .
As long as the scroll view you want to customize is not part of a UICollectionView, UITableView, or similar class, then you should be able to set up a gesture recognizer delegate. Inside the delegate, you can use the velocityInView (_ :) UIPanGestureRecognizer method to decide if you want to allow the gesture recognizer to trigger scrolling.
source to share
You can try to place a clear UIView above the scrollView and then add a UIPanGestureRecognizer to it. You will then be able to set a selector for that GestureRecognizer, which can then scroll through the scroll. But then you will be responsible for determining the scrolling distance for each high speed pan.
source to share
You can also implement touchhesMoved method here. You can make a comparison between touches to test the speed. something like this, but this 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 speed for movement that you can use to provide further action.
source to share