Appearance
XtfViewAnimation
XtfViewAnimation 基于 UniElement.animate 封装单目标、并行和序列动画。它是独立工具,不计入 112 个组件;当前源码包含 101 个 preset。
1. 基础播放
vue
<template>
<view class="animation-basic">
<view ref="target" class="animation-target"><text class="animation-text">账单</text></view>
<button class="animation-button" @click="play">播放淡入</button>
</view>
</template>
<script setup lang="uts">
import { ref } from "vue"
import { XtfViewAnimation } from "@/uni_modules/xtf-linkuiunix/libs/utils/xtf-view-animation.uts"
const target = ref<UniElement | null>(null)
let animation: UniAnimation | null = null
function play(): void { animation = XtfViewAnimation.play(target.value, "fade-in-up", { duration: 360, onFinish: () => console.log("基础动画完成") }) }
</script>
<style>
.animation-basic { display: flex; flex-direction: column; padding: 24rpx; }
.animation-target { display: flex; flex-direction: row; align-items: center; justify-content: center; height: 180rpx; background-color: #e0f2fe; }
.animation-text { color: #075985; font-size: 32rpx; }
.animation-button { margin-top: 16rpx; }
</style>2. 并行动画
vue
<template>
<view class="animation-parallel">
<view class="animation-row"><view ref="left" class="animation-box"><text class="animation-text">收入</text></view><view ref="right" class="animation-box"><text class="animation-text">支出</text></view></view>
<button class="animation-button" @click="playParallel">并行播放</button>
</view>
</template>
<script setup lang="uts">
import { ref } from "vue"
import { XtfViewAnimation, type XtfViewAnimationTask } from "@/uni_modules/xtf-linkuiunix/libs/utils/xtf-view-animation.uts"
const left = ref<UniElement | null>(null)
const right = ref<UniElement | null>(null)
let animations = [] as UniAnimation[]
function playParallel(): void {
XtfViewAnimation.cancelAll(animations)
const tasks = [{ element: left.value, preset: "bounce-in-left", options: { duration: 420 } }, { element: right.value, preset: "rotate-in", options: { duration: 420, onFinish: () => console.log("并行动画完成") } }] as XtfViewAnimationTask[]
animations = XtfViewAnimation.playParallel(tasks)
}
</script>
<style>
.animation-parallel { display: flex; flex-direction: column; padding: 24rpx; }
.animation-row { display: flex; flex-direction: row; justify-content: space-between; }
.animation-box { display: flex; flex-direction: row; align-items: center; justify-content: center; width: 46%; height: 140rpx; background-color: #dcfce7; }
.animation-text { color: #166534; font-size: 28rpx; }
.animation-button { margin-top: 16rpx; }
</style>3. 序列与暂停
vue
<template>
<view class="animation-sequence">
<view ref="target" class="animation-sequence-target"><text class="animation-text">同步完成</text></view>
<view class="animation-controls"><button class="animation-control" @click="start">开始序列</button><button class="animation-control" @click="pause">暂停</button><button class="animation-control" @click="resume">继续</button></view>
</view>
</template>
<script setup lang="uts">
import { ref } from "vue"
import { XtfViewAnimation, type XtfViewAnimationSequence, type XtfViewAnimationSequenceStep } from "@/uni_modules/xtf-linkuiunix/libs/utils/xtf-view-animation.uts"
const target = ref<UniElement | null>(null)
let sequence: XtfViewAnimationSequence | null = null
const steps = [{ preset: "fade-in-up", options: { duration: 320 } }, { preset: "pulse", options: { duration: 320 } }, { preset: "fade-out-down", options: { duration: 320, onFinish: () => console.log("序列完成") } }] as XtfViewAnimationSequenceStep[]
function start(): void { XtfViewAnimation.cancelSequence(sequence); sequence = XtfViewAnimation.playSequence(target.value, steps) }
function pause(): void { XtfViewAnimation.pauseSequence(sequence) }
function resume(): void { XtfViewAnimation.resumeSequence(sequence) }
</script>
<style>
.animation-sequence { display: flex; flex-direction: column; padding: 24rpx; }
.animation-sequence-target { display: flex; flex-direction: row; align-items: center; justify-content: center; height: 160rpx; background-color: #fef3c7; }
.animation-controls { display: flex; flex-direction: row; margin-top: 16rpx; }
.animation-control { margin-right: 12rpx; }
.animation-text { color: #92400e; font-size: 28rpx; }
</style>方法签名
| 方法 | 签名 | 用途 |
|---|---|---|
play | play(element: UniElement | null, preset: string, options: XtfViewAnimationOptions | null = null): UniAnimation | null | 播放单个 preset;目标为空或 preset 不存在时返回 null。 |
cancel/pause/resume/finish | (animation: UniAnimation | null): void | 控制单个动画实例。 |
reset | reset(element: UniElement | null, animation: UniAnimation | null = null): void | 取消动画并清除 opacity、transform。 |
playParallel | playParallel(tasks: XtfViewAnimationTask[]): UniAnimation[] | 同时启动多个目标任务。 |
cancelAll | cancelAll(animations: UniAnimation[]): void | 取消并行动画数组。 |
playSequence | playSequence(element: UniElement | null, steps: XtfViewAnimationSequenceStep[], loop: boolean = false): XtfViewAnimationSequence | 顺序执行步骤并返回控制器。 |
cancelSequence/pauseSequence/resumeSequence | (sequence: XtfViewAnimationSequence | null): void | 取消、暂停或恢复序列。 |
XtfViewAnimationOptions
| 字段 | 类型 | 作用 |
|---|---|---|
duration | number | null | 动画时长,默认 560ms。 |
delay | number | null | 播放延迟。 |
easing | UniAnimationOptionEasing | null | 缓动函数。 |
iterations | number | null | 播放次数,可传 Infinity。 |
direction | UniAnimationOptionDirection | null | 播放方向。 |
fill | UniAnimationOptionFill | null | 结束帧保持方式。 |
onStart/onFinish/onCancel | () => void | 生命周期回调。 |
编排结构
XtfViewAnimationTask:element、preset、可选options。XtfViewAnimationSequenceStep:preset、可选options。XtfViewAnimationSequence:保存animations/cancelled/paused/finished/loop等运行状态,由序列控制 API 使用。
Preset
fade-infade-outfade-in-upfade-in-downfade-in-leftfade-in-rightfade-out-upfade-out-downfade-out-leftfade-out-rightfade-in-smallfade-in-largefade-out-smallfade-out-largefade-slide-in-upfade-slide-in-downfade-slide-in-leftfade-slide-in-rightfade-slide-out-upfade-slide-out-downfade-slide-out-leftfade-slide-out-rightslide-in-upslide-in-downslide-in-leftslide-in-rightslide-out-upslide-out-downslide-out-leftslide-out-rightzoom-inzoom-outzoom-in-upzoom-in-downzoom-in-leftzoom-in-rightzoom-out-upzoom-out-downzoom-out-leftzoom-out-rightrotate-inrotate-outrotate-in-leftrotate-in-rightrotate-out-leftrotate-out-rightflip-xflip-yflip-in-xflip-out-xflip-in-yflip-out-yroll-inroll-outlight-speed-in-leftlight-speed-in-rightlight-speed-out-leftlight-speed-out-rightbouncebounce-inbounce-outbounce-in-upbounce-in-downbounce-in-leftbounce-in-rightbounce-out-upbounce-out-downbounce-out-leftbounce-out-rightpulseheartbeatflashrubber-bandshake-xshake-yswingtadawobblejellospinspin-reversepop-inpop-outdrop-indrop-outfloatfloat-xdrift-leftdrift-rightbreatheblinkpendulumrocktilt-in-lefttilt-in-righttilt-out-lefttilt-out-rightskew-inskew-outcompress-incompress-out
当前 Demo 验证了代表 preset、并行和序列的主要流程;preset 映射存在不等于每个效果已在所有原生平台完成视觉验收。