Skip to content

Instantly share code, notes, and snippets.

@wagenet
Created August 24, 2010 18:21
Show Gist options
  • Save wagenet/548030 to your computer and use it in GitHub Desktop.
Save wagenet/548030 to your computer and use it in GitHub Desktop.
MiniApp.Tappable = {
_tap_tapCount: null,
_tap_candidateTouch: null,
_tap_eventTimer: null,
tapWiggle: 10,
tapDelay: 200,
tapStart: function(evt){
var viewTouches = evt.touchesForView(this),
shouldCancel = NO;
// We don't want events triggering during a touch, will be reset when touch is over if it's a candidate
if (this._tap_eventTimer) this._tap_eventTimer.invalidate();
// We have an activeTap but another touch has been started
if (this._tap_tapCount && viewTouches) shouldCancel = YES;
if (shouldCancel) {
this._tap_cancelTap();
return;
}
// This touch is a candidate
this._tap_candidateTouch = {
startTime: Date.now(),
identifier: evt.identifier
}
},
tapsChanged: function(evt, viewTouches){
var touch = viewTouches[0];
// Somehow another touch got in
var tooManyTouches = (
viewTouches.length > 1 ||
!this._tap_candidateTouch ||
touch.identifier !== this._tap_candidateTouch.identifier
);
// Touch moved too much
var touchMoved = touch.dragDistance() > this.get('tapWiggle');
if (tooManyTouches || touchMoved) this._tap_cancelTap();
},
tapEnd: function(evt){
if (this._tap_candidateTouch) {
if (evt.dragDistance() > this.get('tapWiggle') || Date.now() - this._tap_candidateTouch.startTime > this.get('tapDelay') ) {
// Touch moved too much or took too long
this._tap_cancelTap();
} else {
this._tap_addTap();
}
}
},
_tap_addTap: function(){
if (this._tap_eventTimer) this._tap_eventTimer.invalidate();
this._tap_tapCount = (this._tap_tapCount || 0) + 1;
this._tap_candidateTouch = null;
this._tap_eventTimer = SC.Timer.schedule({
target: this,
action: '_tap_triggerTap',
interval: this.get('tapDelay')
});
},
_tap_cancelTap: function(){
if (this._tap_eventTimer) this._tap_eventTimer.invalidate();
this._tap_tapCount = null;
this._tap_candidateTouch = null;
this._tap_eventTimer = null;
},
_tap_triggerTap: function(){
this.tap(this._tap_tapCount);
this._tap_tapCount = null;
this._tap_candidateTouch = null;
this._tap_eventTimer = null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment