Skip to content

Commit

Permalink
velocity tracker returns invalid values
Browse files Browse the repository at this point in the history
  • Loading branch information
sephiroth74 committed Jan 8, 2019
1 parent b4f06ca commit 18e04ea
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,8 @@ class TestSwipeGesture : TestBaseClass() {
recognizer.direction = UISwipeGestureRecognizer.RIGHT
recognizer.actionListener = actionListener

val recognizer2 = UISwipeGestureRecognizer(context)
recognizer2.tag = "swipe-up"
recognizer2.direction = UISwipeGestureRecognizer.UP
val recognizer2 = UITapGestureRecognizer(context)
recognizer2.tag = "tap"
recognizer2.actionListener = {
fail("unexpected")
}
Expand All @@ -216,7 +215,7 @@ class TestSwipeGesture : TestBaseClass() {
delegate.addGestureRecognizer(recognizer)
delegate.addGestureRecognizer(recognizer2)

mainView.swipeRight(5)
mainView.swipeRight(10)

latch.await(2, TimeUnit.SECONDS)
assertEquals(0L, latch.count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ abstract class UIGestureRecognizer(context: Context) : OnGestureRecognizerStateC
if (!sDebug) {
return
}
Log.println(level, LOG_TAG, "[${javaClass.simpleName}] $fmt")
Log.println(level, LOG_TAG, "[${javaClass.simpleName}:$tag] $fmt")
}

@Suppress("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ open class UISwipeGestureRecognizer(context: Context) : UIGestureRecognizer(cont
mVelocityTracker = VelocityTracker.obtain()
}

mVelocityTracker!!.addMovement(event)
mVelocityTracker?.addMovement(event)

when (action) {
MotionEvent.ACTION_POINTER_DOWN -> {
Expand All @@ -213,7 +213,7 @@ open class UISwipeGestureRecognizer(context: Context) : UIGestureRecognizer(cont
mLastFocusLocation.set(mCurrentLocation)
mDownFocusLocation.set(mCurrentLocation)

mVelocityTracker!!.computeCurrentVelocity(1000, scaledMaximumFlingVelocity.toFloat())
mVelocityTracker?.computeCurrentVelocity(1000, scaledMaximumFlingVelocity.toFloat())
val upIndex = event.actionIndex

val id1 = event.getPointerId(upIndex)
Expand All @@ -231,7 +231,7 @@ open class UISwipeGestureRecognizer(context: Context) : UIGestureRecognizer(cont
val dot = x + y

if (dot < 0) {
mVelocityTracker!!.clear()
mVelocityTracker?.clear()
break
}
}
Expand All @@ -252,7 +252,7 @@ open class UISwipeGestureRecognizer(context: Context) : UIGestureRecognizer(cont
mLastFocusLocation.set(mCurrentLocation)
mDownFocusLocation.set(mCurrentLocation)

mVelocityTracker!!.clear()
mVelocityTracker?.clear()

setBeginFiringEvents(false)
removeMessages(MESSAGE_RESET)
Expand All @@ -264,14 +264,15 @@ open class UISwipeGestureRecognizer(context: Context) : UIGestureRecognizer(cont
scrollX = mLastFocusLocation.x - mCurrentLocation.x
scrollY = mLastFocusLocation.y - mCurrentLocation.y

mVelocityTracker?.computeCurrentVelocity(1000, scaledMaximumFlingVelocity.toFloat())
yVelocity = mVelocityTracker!!.yVelocity
xVelocity = mVelocityTracker!!.xVelocity

if (state == State.Possible) {
val distance = mCurrentLocation.distance(mDownFocusLocation)
logMessage(Log.INFO, "started: $mStarted, distance: $distance, slop: $scaledTouchSlop")
if (!mStarted) {
if (distance > scaledTouchSlop) {
mVelocityTracker!!.computeCurrentVelocity(1000, scaledMaximumFlingVelocity.toFloat())
yVelocity = mVelocityTracker!!.yVelocity
xVelocity = mVelocityTracker!!.xVelocity

translationX -= scrollX
translationY -= scrollY
Expand Down Expand Up @@ -325,9 +326,6 @@ open class UISwipeGestureRecognizer(context: Context) : UIGestureRecognizer(cont
}
} else {
// touch has been recognized. now let's track the movement
mVelocityTracker!!.computeCurrentVelocity(1000, scaledMaximumFlingVelocity.toFloat())
yVelocity = mVelocityTracker!!.yVelocity
xVelocity = mVelocityTracker!!.xVelocity
val time = event.eventTime - event.downTime

if (time > maximumTouchFlingTime) {
Expand Down Expand Up @@ -378,6 +376,8 @@ open class UISwipeGestureRecognizer(context: Context) : UIGestureRecognizer(cont
}

MotionEvent.ACTION_UP -> {
mVelocityTracker?.addMovement(event)

if (mVelocityTracker != null) {
mVelocityTracker!!.recycle()
mVelocityTracker = null
Expand Down

0 comments on commit 18e04ea

Please sign in to comment.