动画高度属性

时间:2018-11-01 16:09:32

标签: nativescript nativescript-vue

你好,有stackoverflow和nativescript用户!

我对动画设置视图有疑问。

我想要的是:我正在尝试创建一个可以单击的视图,它会在动画下方打开一个新视图,将所有其他视图进一步向下推。

问题:仅对内部元素进行动画处理,或不对动画进行处理 完全没有。

尝试的方法:我尝试使用nativescript UI Animations,但由于不支持height属性,因此未成功。

我的版本:https://gyazo.com/130c2b3467656bcc104c9b8e2c860d94

我很想听听大家提出的解决方案。

1 个答案:

答案 0 :(得分:2)

您可以在本机脚本应用程序中使用tweenjs,定义依赖于tweenjs的以下类(使用npm i @tweenjs/tween.js安装)

import * as TWEEN from '@tweenjs/tween.js';
export { Easing } from '@tweenjs/tween.js';

TWEEN.now = function () {
    return new Date().getTime();
};

export class Animation extends TWEEN.Tween {
    constructor(obj) {
        super(obj);
        this['_onCompleteCallback'] = function() {
            cancelAnimationFrame();
        };
    }
    start(time) {
        startAnimationFrame();
        return super.start(time);
    }
}

let animationFrameRunning = false;
const cancelAnimationFrame = function() {
    runningTweens--;
    if (animationFrameRunning && runningTweens === 0) {
        animationFrameRunning = false;
    }
};

let runningTweens = 0;
const startAnimationFrame = function() {
    runningTweens++;
    if (!animationFrameRunning) {
        animationFrameRunning = true;
        tAnimate();
    }
};
const requestAnimationFrame = function(cb) {
    return setTimeout(cb, 1000 / 60);
};
function tAnimate() {
    if (animationFrameRunning) {
        requestAnimationFrame(tAnimate);
        TWEEN.update();
    }
}

然后,要设置视图的高度动画,您可以使用这种方法(此方法在nativescript-vue中有效,但是您只需调整检索视图对象的方式即可):

import {Animation, Easing} from "./Animation"

toggle() {
    let view = this.$refs.panel.nativeView
    if (this.showPanel) {
        new Animation({ height: this.fixedHeight })
            .to({ height: 0 }, 500)
            .easing(Easing.Back.In)
            .onUpdate(obj => {
                view.originY = 0
                view.scaleY = obj.height / this.fixedHeight;
                view.height = obj.height;
            })
            .start()
            .onComplete(() => this.showPanel = !this.showPanel);
    } else {
        this.showPanel = !this.showPanel
        new Animation({ height: 0 })
            .to({ height: this.fixedHeight }, 500)
            .easing(Easing.Back.Out)
            .onUpdate(obj => {
                view.originY = 0
                view.scaleY = obj.height / this.fixedHeight;
                view.height = obj.height;
            })
            .start();
    }
}

在此处进行了讨论:https://github.com/NativeScript/NativeScript/issues/1764 我主要改进了onUpdate使其具有平滑的动画

相关问题