Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Getting touch x and y coordinates on 0.8.0

Hello
I'm trying to develop an app that will work on most iPhone os versions.
I've noticed that since 0.8.1 there's no support for os 3.0 and below so I wanted to create the app using TD 0.8.0.

I couldn't find the original docs for that version.

What I'm trying to achieve is to get the x,y coordinates of the first touch/swipe/drag the user makes.
I've tried using transparent PNG's for buttons but I get the pressed effect on them which is a problem.

Is there a way to get the touch x,y coordinates like in later versions?
Or to suppress the effect on the buttons?

— asked June 7th 2010 by Shahar Zrihen
  • 0.8.0
  • iphone
0 Comments

4 Answers

  • If you're using 0.8.x, the best source of documentation on touch events actually comes from the Safari dev center - there are docs out there on Webkit's touch events, which you can use for this purpose. Bear in mind that 0.8.x will not be receiving any upgrades from Appcelerator, though…

    — answered June 7th 2010 by Kevin Whinnery
    permalink
    0 Comments
  • In a webView you can use :

      var touch=e.touches[0];
      var x=touch.clientX;
      var y=touch.clientY;
    

    where e come from :

    yourIdElement.ontouchstart=function(e)
    

    but there is also : ontouchmove, ontouchend etc…

    — answered June 8th 2010 by Stephane Pelamourgues
    permalink
    1 Comment
    • Thanks for the quick replies guys!
      I would rather solve it under Titanium if I can as I'm making an iPad version of the same app and want to keep the code as similar as possible.

      I've tried stephane's answer but couldn't get it to work.
      I've used this code -

      var node = document.getElementById('wrapper');
      node.ontouchmove = function(e){
        if(e.touches.length == 1){ // Only deal with one finger
          var touch = e.touches[0]; // Get the information for finger #1
          var node = touch.target; // Find the node the drag started from
          var alerty = Titanium.UI.createAlertDialog();
              alerty.setTitle("x = " + touch.pageX + " y=" + touch.pageY );
              alerty.show();
        }
      };
      

      with and without the getElementById but to no success.
      Can you please point me to my mistake?

      Shahar

      — commented June 8th 2010 by Shahar Zrihen
  • I've managed to find the answer through other forums.
    It's done through regular touch events and not TI events but it works fine.
    I've enclosed the code just in case someone else will be looking for this.

    document.addEventListener('touchstart', function(event) {
        event.preventDefault();
        var touch = event.touches[0];
        var a = Titanium.UI.createAlertDialog({
        title:'Alert Test',
        message:"Touch x:" + touch.pageX + ", y:" + touch.pageY});
    a.show();
    }, false);
    

    Shahar

    — answered June 8th 2010 by Shahar Zrihen
    permalink
    0 Comments
  • do any one knows how to use titanium to build tic tac game for android

    — answered September 12th 2012 by Nnamdi Nwosu
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.