您可以在您的 nativescript 应用程序中使用 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
动画流畅