Titanium Community Questions & Answer Archive

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

Spinning images and drag&drop

Hi

Both Spinning images and drag&drop sample code is missing from the Kitchin sink app.

This small code demo shows how to spin an image using a transform and an animation and how to drag&drop images on iPhone and iPad.

I have almost finished an iPhone/iPad game that uses drag and drop and spinning images that I would like to get it working on android. I don't have a android handset. Can someone see if they can get my sample code to work on an android phone and paste the changes as a reply.

Thanks

// Just past me into into a blank app.js file and run
window = Titanium.UI.createWindow({
    backgroundColor:'#47a',
    exitOnClose: true
});

//load test image from the appcelerator web site
var image = Titanium.UI.createImageView({
   image:'http://www.appcelerator.com/wp-content/themes/appcelerator/img/HERO_iPh_social.png',
   backgroundColor:'transparent',
   height:'auto',
   width:'auto'
});

// Spin the image
var t = Ti.UI.create2DMatrix();
var spin = Titanium.UI.createAnimation();
t = t.rotate(3);
spin.transform = t;
spin.duration = 50;
spin.addEventListener('complete', function(e) {
    t = t.rotate(3);
    spin.transform = t;
    image.animate(spin);
});

//make the image dragable
image.addEventListener('touchstart', function (e) {
    image.ox = e.globalPoint.x - image.center.x;
    image.oy = e.globalPoint.y - image.center.y;
});
image.addEventListener('touchmove', function (e) {
    Ti.API.info(JSON.stringify(e));
    image.center = {
        x:(e.globalPoint.x - image.ox),
        y:(e.globalPoint.y - image.oy)
    };
});

window.add(image);
image.animate(spin);
window.open({fullscreen:true});
— asked September 3rd 2010 by Tom Campbell
  • android
  • animation
  • drag
  • drop
  • ipad
  • iphone
  • spin
1 Comment
  • the old image has been deleted try this as the image URL

    http://static.appcelerator.net/images/header/appc_logo.png

    — commented June 6th 2012 by Tom Campbell

4 Answers

  • Accepted Answer

    Here's what I came up with, recycling the code to use in any orientation:

    // Just past me into into a blank app.js file and run
    window = Titanium.UI.createWindow({
        backgroundColor:'#47a',
        exitOnClose: true
    });
    
    window.orientation = Titanium.UI.PORTRAIT;
    Ti.Gesture.addEventListener('orientationchange',function(e){
        window.orientation = e.orientation;
    });
    
    //load test image from the appcelerator web site
    var image = Titanium.UI.createImageView({
       image:'http://www.appcelerator.com/wp-content/themes/appcelerator/img/HERO_iPh_social.png',
       backgroundColor:'transparent',
       height:'auto',
       width:'auto'
    });
    
    // Spin the image
    var t = Ti.UI.create2DMatrix();
    var spin = Titanium.UI.createAnimation();
    t = t.rotate(3);
    spin.transform = t;
    spin.duration = 50;
    spin.addEventListener('complete', function(e) {
        t = t.rotate(3);
        spin.transform = t;
        image.animate(spin);
    });
    
    //make the image dragable
    image.addEventListener('touchstart', function (e) {
        Ti.API.info('width: '+ window.width +' height: '+ window.height);
        switch(window.orientation)
        {
            // UPSIDE_PORTRAIT
            case 2:
                image.ox = window.width - e.globalPoint.x - image.center.x;
                image.oy = window.height - e.globalPoint.y - image.center.y;
                break;
            // LANDSCAPE_LEFT
            case 3:
                image.ox = e.globalPoint.y - image.center.x;
                image.oy = window.height - e.globalPoint.x - image.center.y;
                break;
            // LANDSCAPE_RIGHT
            case 4:
                image.ox = window.width - e.globalPoint.y - image.center.x;
                image.oy = e.globalPoint.x - image.center.y;
                break;
            // PORTRAIT
            default:
                image.ox = e.globalPoint.x - image.center.x;
                image.oy = e.globalPoint.y - image.center.y;
                break;
        }
    });
    image.addEventListener('touchmove', function (e) {
        Ti.API.info(JSON.stringify(e));
    
        switch(window.orientation)
        {
            // UPSIDE_PORTRAIT
            case 2:
                image.center = {
                    x:(window.width - e.globalPoint.x - image.ox),
                    y:(window.height - e.globalPoint.y - image.oy)
                };
                break;
            // LANDSCAPE_LEFT
            case 3:
                image.center = {
                    x:(e.globalPoint.y - image.ox),
                    y:(window.height - e.globalPoint.x - image.oy)
                };
                break;
            // LANDSCAPE_RIGHT
            case 4:
                image.center = {
                    x:(window.width - e.globalPoint.y - image.ox),
                    y:(e.globalPoint.x - image.oy)
                };
                break;
            // PORTRAIT
            default:
                image.center = {
                    x:(e.globalPoint.x - image.ox),
                    y:(e.globalPoint.y - image.oy)
                };
                break;
        }
    });
    
    window.add(image);
    image.animate(spin);
    window.open({fullscreen:true});
    
    — answered October 4th 2010 by Marco Ferreira
    permalink
    0 Comments
  • GREAT CODE.
    BUT…When I paste your dragable function into my own app.

    the drag is inverted.

    Touchmove up is drag down and left is right etc etc.

    Do you know a fix for this

    EDIT: It happens because the app is made in landscape, I guess its the same bug as with the swipe event…which will be fixed in 1.5.0

    or is there a temp solution?

    — answered September 29th 2010 by Patrick van Zadel
    permalink
    0 Comments
  • Doesn't work in Android. Doesn't move and doesn't drag.

    — answered September 29th 2010 by Filip Dimitrovski
    permalink
    0 Comments
  • I tested on the android emulator and the is no movement or spinning. Hopefully 1.5 will fix this.

    I used globalPoint in the touch events which only works in portrait orientation as it returns absolute device pixels. You can try switching to using view based x and y co-ordinates by removing the ".globalPoint" from the code.

    Tom

    — answered September 29th 2010 by Tom Campbell
    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.