Skip to content

Commit

Permalink
tap
Browse files Browse the repository at this point in the history
  • Loading branch information
sephiroth74 committed Jan 6, 2019
1 parent bc4b2c5 commit 55d4a5b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ class MainActivity : AppCompatActivity() {
mDelegate = UIGestureRecognizerDelegate()

val recognizer1 = UITapGestureRecognizer(this)
recognizer1.tapsRequired = 1
recognizer1.touchesRequired = 1
recognizer1.tag = "single-tap"
recognizer1.tapsRequired = 2
recognizer1.touchesRequired = 2
recognizer1.actionListener = actionListener
recognizer1.stateListener = stateListener

Expand All @@ -37,7 +36,6 @@ class MainActivity : AppCompatActivity() {
mRoot.setGestureDelegate(mDelegate)

mDelegate.shouldReceiveTouch = { true }

mDelegate.shouldBegin = { true }

mDelegate.shouldRecognizeSimultaneouslyWithGestureRecognizer = { recognizer, other ->
Expand All @@ -58,21 +56,22 @@ class MainActivity : AppCompatActivity() {
mRoot = findViewById(R.id.activity_main)
}

val runner = Runnable {
private val runner = Runnable {
text2.text = ""
text.text = ""
text3.text = ""
}

val stateListener =
private val stateListener =
{ recognizer: UIGestureRecognizer, oldState: UIGestureRecognizer.State?, newState: UIGestureRecognizer.State? ->
Timber.d("onStateChanged: $oldState --> $newState")
text3.text = "${recognizer.javaClass.simpleName}: $oldState --> $newState"
}

val actionListener = { recognizer: UIGestureRecognizer ->
private val actionListener = { recognizer: UIGestureRecognizer ->
val dateTime = dateFormat.format(recognizer.lastEvent!!.eventTime)
Timber.d("**********************************************")
Timber.d("onGestureRecognized($recognizer)")
Timber.d("**********************************************")

text.text = "${recognizer.javaClass.simpleName}: ${recognizer.state}"
text2.append("[$dateTime] tag: ${recognizer.tag}, state: ${recognizer.state?.name} \n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class TestLongPressGesture : TestBaseClass() {
recognizer.tag = "long-press"
recognizer.actionListener = { it ->
activity.actionListener.invoke(it)

fail("not expected")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ abstract class UIGestureRecognizer(context: Context) : OnGestureRecognizerStateC
// compute current location
mNumberOfTouches = computeFocusPoint(event, mCurrentLocation)

logMessage(Log.VERBOSE, "event.action: ${event.action}, focusPoint: $mCurrentLocation")
if (logEnabled) {
logMessage(Log.VERBOSE, "event.action: ${eventActionToString(event.actionMasked)}, focusPoint:$mCurrentLocation, " +
"eventPoint:${PointF(event.x, event.y)}")
}
return false
}

Expand Down Expand Up @@ -364,5 +367,17 @@ abstract class UIGestureRecognizer(context: Context) : OnGestureRecognizerStateC
set(value) {
sDebug = value
}

fun eventActionToString(action: Int): String {
return when (action) {
MotionEvent.ACTION_DOWN -> "ACTION_DOWN"
MotionEvent.ACTION_UP -> "ACTION_UP"
MotionEvent.ACTION_CANCEL -> "ACTION_CANCEL"
MotionEvent.ACTION_MOVE -> "ACTION_MOVE"
MotionEvent.ACTION_POINTER_DOWN -> "ACTION_POINTER_DOWN"
MotionEvent.ACTION_POINTER_UP -> "ACTION_POINTER_UP"
else -> "ACTION_OTHER"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,37 @@ open class UILongPressGestureRecognizer(context: Context) : UIGestureRecognizer(

/**
* @return The maximum allowed movement of the fingers on the view before the gesture fails.
* @since 1.0.0
* @since 1.2.4
*/
var allowableMovement: Float
var allowableMovement: Int

/**
* Minimum movement before we start collecting movements
* @since 1.2.5
*/
var touchSlop: Int
var scaledTouchSlop: Int

/**
* the duration in milliseconds between the first tap's up event and the second tap's down
* event for an interaction to be considered a double-tap
* @since 1.2.5
*/
var scaledDoubleTapSlop: Int

init {
mStarted = false
mBegan = false

val configuration = ViewConfiguration.get(context)

allowableMovement = configuration.scaledDoubleTapSlop.toFloat() / 2
touchSlop = configuration.scaledTouchSlop
scaledTouchSlop = configuration.scaledTouchSlop
scaledDoubleTapSlop = configuration.scaledDoubleTapSlop
allowableMovement = configuration.scaledTouchSlop

if (logEnabled) {
logMessage(Log.INFO, "allowableMovement: $allowableMovement")
logMessage(Log.INFO, "touchSlop: $touchSlop")
logMessage(Log.INFO, "scaledTouchSlop: $scaledTouchSlop")
logMessage(Log.INFO, "scaledDoubleTapSlop: $scaledDoubleTapSlop")
logMessage(Log.INFO, "longPressTimeout: $longPressTimeout")
logMessage(Log.INFO, "doubleTapTimeout: $doubleTapTimeout")
}
Expand Down Expand Up @@ -176,6 +185,15 @@ open class UILongPressGestureRecognizer(context: Context) : UIGestureRecognizer(
mStarted = true
} else {
mNumTaps++

// if second tap is too far wawy from the first
val distance = mDownLocation.distance(mPreviousDownLocation)
logMessage(Log.VERBOSE, "distance: $distance")
if (distance > scaledDoubleTapSlop) {
logMessage(Log.WARN, "second touch too far away ($distance > $scaledDoubleTapSlop)")
handleFailed()
return cancelsTouchesInView
}
}

logMessage(Log.VERBOSE, "num taps: $mNumTaps, tapsRequired: $tapsRequired")
Expand Down Expand Up @@ -249,9 +267,9 @@ open class UILongPressGestureRecognizer(context: Context) : UIGestureRecognizer(
} else if (state == State.Began) {
if (!mBegan) {
val distance = mCurrentLocation.distance(mDownFocusLocation)
logMessage(Log.VERBOSE, "distance: $distance, allowableMovement: $touchSlop")
logMessage(Log.VERBOSE, "distance: $distance, allowableMovement: $scaledTouchSlop")

if (distance > touchSlop) {
if (distance > scaledTouchSlop) {
mBegan = true

if (hasBeganFiringEvents()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ open class UITapGestureRecognizer(context: Context) : UIGestureRecognizer(contex
}
}

private val mPreviousTapLocation = PointF()

override fun onTouchEvent(event: MotionEvent): Boolean {
super.onTouchEvent(event)

Expand Down Expand Up @@ -156,13 +158,17 @@ open class UITapGestureRecognizer(context: Context) : UIGestureRecognizer(contex
mNumTaps = 0
mStarted = true
} else {

// if second tap is too far wawy from the first
val distance = mDownLocation.distance(mPreviousDownLocation)
logMessage(Log.VERBOSE, "distance: $distance")
if (distance > scaledDoubleTapSlop) {
logMessage(Log.WARN, "second touch too far away ($distance > $scaledDoubleTapSlop)")
handleFailed()
return cancelsTouchesInView
// and only 1 finger is required
if (touchesRequired == 1 && tapsRequired > 1) {
val distance = mDownLocation.distance(mPreviousDownLocation)
logMessage(Log.VERBOSE, "distance: $distance")
if (distance > scaledDoubleTapSlop) {
logMessage(Log.WARN, "second touch too far away ($distance > $scaledDoubleTapSlop)")
handleFailed()
return cancelsTouchesInView
}
}
}

Expand All @@ -180,6 +186,22 @@ open class UITapGestureRecognizer(context: Context) : UIGestureRecognizer(contex
if (numberOfTouches > touchesRequired) {
logMessage(Log.WARN, "too many touches: $numberOfTouches > $touchesRequired")
state = State.Failed

} else if (numberOfTouches == touchesRequired && tapsRequired > 1) {
// let's check if the current tap is too far away from the previous
if (mNumTaps < tapsRequired) {
mPreviousTapLocation.set(mCurrentLocation)
} else if (mNumTaps == tapsRequired) {
val distance = mCurrentLocation.distance(mPreviousTapLocation)

// moved too much from the previous tap
if (distance > scaledDoubleTapSlop) {
logMessage(Log.WARN, "distance is $distance > $scaledDoubleTapSlop")
handleFailed()
return cancelsTouchesInView
}
}

}
}

Expand All @@ -199,9 +221,11 @@ open class UITapGestureRecognizer(context: Context) : UIGestureRecognizer(contex
if (mAlwaysInTapRegion) {
val distance = mDownFocus.distance(mCurrentLocation)

logMessage(Log.VERBOSE, "distance: $distance, slop: $scaledTouchSlop")
// if taps and touches > 1 then we need to be less strict
val slop = if (touchesRequired > 1 && tapsRequired > 1) scaledDoubleTapSlop else scaledTouchSlop

if (distance > scaledTouchSlop) {
if (distance > slop) {
logMessage(Log.WARN, "distance: $distance, slop: $slop")
logMessage(Log.WARN, "moved too much")
mAlwaysInTapRegion = false
removeMessages()
Expand Down

0 comments on commit 55d4a5b

Please sign in to comment.