跳转到正文
jolt.
← 动效配方

布局 / iOS / NATIVESCRIPT VUE

一个时钟,联动场景。

页面指示器与引导翻页共用同一个时钟。

来自 Auralis ↗
原生录屏 · iOSAuralis iOS 创作者录屏:动画开场、悬浮玻璃卡片、三个引导场景和页面控件。原始速度,无音频;省略最初 4.25 秒的系统主屏幕与启动转场。录制设备未说明;非性能测试。账户和服务均为示意。

动效背后的代码。

Jolt 0.1.0-rc.11

来自项目的完整源文件,固定到已核对的版本。请结合下面的依赖说明进行适配。

在 NativeScript-Vue setup 中调用 useCarouselClock,并传入异步 advance 回调。参照 PageIndicator.vue,将 progress 绑定到指示器的 scaleX,originX 设为 0。导航前调用 hold,场景稳定后调用 start;将 playing/toggle 接到有标签的控件。在 setup 时检测到减少动态效果或屏幕阅读器则默认暂停。保留代次保护、motion scope 和卸载清理。暂停保留当前计时,手动导航重置下次阅读间隔。此 hook 不提供视图、滑动导航或偏好变更监听;协调方式见 useOnboarding.ts。

useCarouselClock.ts
import { onUnmounted, ref } from 'nativescript-vue'
import { isA11yEnabled } from '@nativescript/core/accessibility'
import { motionScope, motionValue, reducedMotion, set, type Animation } from '@rafaelaugusto/jolt'

export const CAROUSEL_DURATION = 4000

/** The indicator and advance share one Jolt clock: no interval or per-frame JS. */
export function useCarouselClock(advance: () => Promise<void>) {
  const progress = motionValue(0)
  const playing = ref(!reducedMotion() && !isA11yEnabled())
  const motion = motionScope()
  let timer: Animation | undefined
  let ready = false
  let generation = 0
  if (!playing.value) motion.suspend()

  function hold() {
    ready = false
    generation++
    timer?.stop()
    timer = undefined
    set(progress, 0)
  }

  function start() {
    hold()
    ready = true
    if (!playing.value || motion.disposed) return
    const current = generation
    timer = motion.animate(progress, 1, { duration: CAROUSEL_DURATION, ease: 'linear' })
    void timer.finished.then(completed => {
      if (current !== generation || !completed || motion.disposed) return
      timer = undefined
      if (ready && playing.value) void advance()
    })
  }

  function toggle() {
    playing.value = !playing.value
    if (playing.value) {
      motion.resume()
      if (ready && !timer) start()
    } else motion.suspend()
  }

  onUnmounted(() => { generation++; motion.dispose() })
  return { progress, playing, start, hold, toggle }
}
查看完整示例 ↗

阅读完整指南 ↗