kmc
kmc

Reputation: 658

Animating dimensions in Sencha Touch 2

I'm trying to animate the height of a dataview, but it's currently just sliding the panel around the viewport instead of keeping it in place and changing it's height. The code is as follows:

Ext.Anim.run(el, 'slide', {
  from: { height: height },
  to: { height: newHeight },
  out: false,
  direction: 'up',
  easing: 'ease-out',
  duration: 1000
});

For instance, height=200, newHeight=100 will result in the dataview dropping immediately so that it's top is at 200px below the viewport, and then animating back to the top of the viewport.

How can I get it to change the height? Thanks.

Upvotes: 4

Views: 4213

Answers (3)

Cam Tullos
Cam Tullos

Reputation: 2577

Here's a clean utility function you can use to accomplish this

function animatron (target, prop, duration, to, from, easing) {
    // return if no target or prop
    if (target == null || prop == null) { return; }

    // defaults
    if (duration == null) { duration = 250; }
    if (to == null) { to = 0; }
    if (from == null) { from = target.getHeight(); }
    if (easing == null) { easing = 'ease-out'; }

    // to property
    var t = {};
    t[prop] = to;

    // from property
    var f = {};
    f[prop] = from;

    // Animation Options
    var opts = {
        duration: duration,
        easing: easing,
        element: target.element,
        from: f,
        preserveEndState: true,
        to: t
    };

    // Animation Object
    var anime = new Ext.fx.Animation(opts);

    // On animationend Event
    anime.on('animationend', function (animation, element, isInterrupted) {
        // Hide the target if the to is 0
        if (to == 0 && (prop == 'height' || prop == 'width')) {
            if (!isInterrupted) { target.hide(); }
        }

        // Update property if width or height
        if (prop == 'height') { target.setHeight(to); }
        if (prop == 'width') { target.setWidth(to); }

        // Dispatch 'animated' event to target
        target.fireEvent('animated', animation, element, to, from, isInterrupted);
    });

    // Show the target if it's hidden and to isn't 0
    if (target.getHidden() == true && to != 0) { target.show(); }

    // Run the animation
    Ext.Animator.run(anime);
}

You can listen for the 'animated' event on the target element

animatron(dataview, 'height', 500, 0);
dataview.addListener('animated', function (animation, element, to, from, isInterrupted) {
    console.log('animation ended');
    console.log('interrupted: '+ isInterrupted);
});

Upvotes: 0

jep
jep

Reputation: 757

Since I can't add comments, I'll have to put this as a separate answer. I just wanted to add to what rdougan said and show how you can catch the animation end event. I find it's necessary in the above situation because Sencha Touch's component.getTop/Left/Height/Width() functions return incorrect values after an animation such as the one shown.

dataview.setHeight(dataview.element.getHeight()); // you may or may not need this

console.log('height before\t', dataview.getHeight());

var a = new Ext.fx.Animation({
    element: dataview.element,
    duration: 500,
    easing: 'ease-in',
    preserveEndState: true,
    from: {
        height: dataview.element.getHeight()
    },
    to: {
        height: 100
    }
});

a.on('animationend', function (animation, element, isInterrupted) {
    console.log('height before\t', dataview.getHeight());
    dataview.setHeight(dataview.element.getHeight());
    console.log('height set\t', dataview.getHeight());
});

Ext.Animator.run(a);

I left in some logging so you can see just what I mean. This example was written against ST 2.1 RC2.

Upvotes: 1

rdougan
rdougan

Reputation: 7225

Try using Ext.Animator.run instead:

Ext.Animator.run({
    element: dataview.element,
    duration: 500,
    easing: 'ease-in',
    preserveEndState: true,
    from: {
        height: dataview.element.getHeight()
    },
    to: {
        height: 100
    }
});

And within a full example:

Ext.application({
    name: 'Sencha',

    launch: function() {
        var dataview = Ext.create('Ext.DataView', {
            fullscreen: true,
            style: 'background:red',
            store: {
                fields: ['text'],
                data: [
                    { text: 'one' },
                    { text: 'two' },
                    { text: 'three' }
                ]
            },
            itemTpl: '{text}'
        });

        Ext.Viewport.add({
            xtype: 'button',
            docked: 'top',
            handler: function() {
                Ext.Animator.run({
                    element: dataview.element,
                    duration: 500,
                    easing: 'ease-in',
                    preserveEndState: true,
                    to: {
                        height: 100
                    },
                    from: {
                        height: dataview.element.getHeight()
                    }
                });
            }
        });
    }
});

Upvotes: 9

Related Questions