To build on yesterday’s tip let’s look at a simple example indicating the direction of our users touch.

# app/assets/javascripts/touch_handler.js.coffee
 
movement =
  element: $('body')
  startingPointX: 0
  startingPointY: 0
 
movement.touching = (e) ->  
  e = e.originalEvent
 
  touchObject = e.changedTouches[0]
 
  point = if e.touches then e.touches[0] else e
  movement.startingPointX = point.pageX
  movement.startingPointY = point.pageY
 
  movement.deltaX = touchObject.pageX - movement.startingPointX
  movement.deltaY = touchObject.pageY - movement.startingPointY
 
  if Math.abs(movement.deltaY) < Math.abs(movement.deltaX)
    console.log "Moving horizontal"
  else
    console.log "Moving vertical"
 
movement.element.bind 'touchmove', movement.touching

Leave a Reply

Your email address will not be published. Required fields are marked *