Titanium Community Questions & Answer Archive

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

Stop animation

Hello

I am trying to stop an animation. I looked into kitchensink app how they do it, but it doesn't seem to work for me.

I use the following code:


var a = Titanium.UI.createAnimation();
a.top = 400;
a.left = 0;
a.duration = 5000;

flag.animate(a, function(){
    clearInterval(interval);
});

var interval = setInterval(function()
{
    if (flag.animatedCenter.y >= 220){
        flag.animate({center:{x:flag.animatedCenter.x,y:flag.animatedCenter.y }});
    };

},10);

Does anyone see what's wrong with it? It just keeps moving.
In the demo app by kitchensink - animation - animation points they use the exact same code on the close button.

Thanks!

— asked March 15th 2011 by Tjeu Vdw
  • animation
  • cancel
  • image
  • moving
  • stop
0 Comments

4 Answers

  • I know this was posted a billion years ago (which are mostly all of the questions/answers I find when I google appcelerator problems) but calling .animate() to stop an animation does not work (anymore?). We are using the latest version .. 3.somethingsomething. I honestly can't find any way of stopping an animation. Anyone knows how you are supposed to do it these days?

    — answered September 2nd 2014 by Albin Larsson
    permalink
    1 Comment
    • I wasn't able to find an answer posted, so here's what I cam up with (in my case, I was rotating a refresh button).

      For iOS it was easy. I just defined a stop animation as:

      var stopRotation = Ti.UI.createAnimation({
        anchorPoint: { x: 0.5, y: 0.5 },
        repeat: 1,
        transform: Ti.UI.create2DMatrix({ rotate: 0 })
      });
      $.refreshButtonIos.animate(stopRotation);
      

      For Android I couldn't find any good way that worked, even after upgrading my project to 3.3.0 and setting overrideCurrentAnimation to true on the button. So instead, when my refresh is complete, my code calls a function that remove that button and replaces it with one that looks identical. The full method is below, including where I set a refreshButton variable based on the OS, and if it's Android I use createStyle so that I can reapply the tss to my new button.

      var refreshButton = (OS_ANDROID) ? $.refreshButtonAndroid : $.refreshButtonIos;
      var refreshButtonStyle = (OS_ANDROID) ? $.createStyle({ classes: ['refreshButton'] }) : null;
      
      var stopRefreshButton = function() {
        if (!isRefreshInProgress) {
          return;
        }
        isRefreshInProgress = false;
      
        if (OS_IOS) {
          refreshButton.animate(stopRotation);
        } else {
          var newRefreshButton = Ti.UI.createButton();
          newRefreshButton.applyProperties(refreshButtonStyle);
          newRefreshButton.addEventListener('singletap', refreshResults);
      
          $.header.remove(refreshButton);
          refreshButton = newRefreshButton;
          $.header.add(refreshButton);
        }
      };
      

      It's admittedly hackish, so hopefully someone will come along eventually and post a way that works using the overrideCurrentAnimation property instead, but this should do the trick for you in the meantime.

      — commented September 8th 2014 by Jason Knisley
  • The KS sample is incorrect. According to the new UICompositeLayout specs, top and left have a higher precedence than center. So trying to stop an animation by defining a center with the top,left,width and height defined is essentially a no op.

    The KS sample was filed as a bug and has been fixed here

    https://github.com/appcelerator-developer-relations/KitchenSink/pull/93

    — answered April 1st 2013 by Vishal Duggal
    permalink
    0 Comments
  • I wasn't able to find an answer posted, so here's what I came up with (in my case, I was rotating a refresh button).
    For iOS it was easy. I just defined a stop animation as:

    var stopRotation = Ti.UI.createAnimation({
      anchorPoint: { x: 0.5, y: 0.5 },
      repeat: 1,
      transform: Ti.UI.create2DMatrix({ rotate: 0 })
    });
    $.refreshButtonIos.animate(stopRotation);
    

    For Android I couldn't find any good way that worked, even after upgrading my project to 3.3.0 and setting overrideCurrentAnimation to true on the button. So instead, when my refresh is complete, my code calls a function that remove that button and replaces it with one that looks identical. The full method is below, including where I set a refreshButton variable based on the OS, and if it's Android I use createStyle so that I can reapply the tss to my new button.

    var refreshButton = (OS_ANDROID) ? $.refreshButtonAndroid : $.refreshButtonIos;
    var refreshButtonStyle = (OS_ANDROID) ? $.createStyle({ classes: ['refreshButton'] }) : null;
    
    var stopRefreshButton = function() {
      if (!isRefreshInProgress) {
        return;
      }
      isRefreshInProgress = false;
    
      if (OS_IOS) {
        refreshButton.animate(stopRotation);
      } else {
        var newRefreshButton = Ti.UI.createButton();
        newRefreshButton.applyProperties(refreshButtonStyle);
        newRefreshButton.addEventListener('singletap', refreshResults);
    
        $.header.remove(refreshButton);
        refreshButton = newRefreshButton;
        $.header.add(refreshButton);
      }
    };
    

    It's admittedly hackish, so hopefully someone will come along eventually and post a way that works using the overrideCurrentAnimation property instead, but this should do the trick for you in the meantime.

    — answered September 8th 2014 by Jason Knisley
    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.