😍
Framer API 中文版
  • 写在前面
  • 从这里开始
    • 介绍
      • 快速开始
      • 基础
    • 教程
      • 准备 Setup
      • 引入 Slider 组件
      • Slider组件的元素
      • 拖动 & MotionValue
      • Hooks & 传递数据
      • 完成
      • 回顾
    • 简要案例
      • Frame
      • Stack
      • Animate
      • Gestures
      • Dragging 拖拽
      • Scrolling 滚动
      • Paging 翻页
      • Tranforms 变换
      • Variants 动画状态组
  • LIBRARY 库
    • Frame
      • Layout 布局
      • Visual 视觉
      • Transform 变换
      • Animation 动画
      • Transition 过渡
      • Variants 动画状态组
      • Tap 点击
      • Hover 悬浮
      • Pan
      • Drag
      • Types
    • Animation
      • Overview
      • Animation controls
      • Tween
      • Spring
      • Inertia 惯性
      • Orchestration
      • Types
    • Color
      • Create
      • Modify
      • Convert
      • Compare
      • Models
    • Page
      • Content
      • Padding
      • Events
      • Effects
      • PageEffectInfo
    • Scroll
      • Sizing
      • Content
      • Events
    • Stack
      • Content
      • Padding
    • Utilities
      • Transfrom
      • useTransform
      • useAnimation
      • useCycle
      • useMotionValue
      • useSpring
      • useViewportScroll
  • FRAMER X
    • Assets
      • Functions
    • Data & Overrides
    • CanvasComponents
      • Canvas.tsx
      • Layout
      • Colors
    • PropertyControls
      • Adding Controls
      • Hiding Controls
      • Array
      • Boolean
      • Color
      • ComponentInstance
      • Enum
      • File
      • FusedNumber
      • Image
      • Number
      • SegmentedEnum
      • String
    • Render Target
      • Properties
      • Functions
Powered by GitBook
On this page

Was this helpful?

  1. LIBRARY 库
  2. Utilities

useCycle

useCycle(c1, c2): [currentState, cycleState]

这个方法能实现在多个视觉属性设置中切换。它能让状态切换时产生自然的动画过渡。它的工作原理有点像React中的useState方法。你需要给这个方法传递几个参数,分别是当前的状态和将要切换的状态,然后它会返回一个数组,内含有两个参数,第一个是当前状态值,第二个是能触发切换的方法。

import * as React from "react"
import { Frame, useCycle } from "framer"

export function MyComponent() {
  const [x, cycleX] = useCycle(0, 50, 100)

  return (
    <Frame
      animate={{ x: x }}
      onTap={() => cycleX()}
     />
   )
}

items: T[]

想要进行切换的几个状态

returns: CycleState<T>

[currentState, cycleState]

Cycling between Variants

useCycle方法经常被用于和variants属性结合进行切换动画效果。

在这个案例中,我们定义了一个有三个属性装啊提的variants的对象,然后使用useCycle方法创建出了一个current变量和cycle方法,然后把current赋值到了Frame组件上的animate属性上,同时给onTap赋值了一个带有cycle方法的匿名函数。

这样的话,我们每次去点击Frame元素的时候,都会触发Frame的背景颜色向下一个variants的属性进行切换,因为你每次点击,current变量里的属性名称都会进行切换。

import * as React from "react"
import { Frame, useCycle } from "framer"

export function MyComponent() {
  const variants = {
    green: { background: "#1ea463" },
    yellow: { background: "#fecd45" },
    red: { background: "#de5347" },
  }

  const [current, cycle] = useCycle(
    "green",
    "yellow",
    "red"
  )

  return (
    <Frame
      variants={variants}
      animate={current}
      onTap={() => cycle()}
    />
  )
}

Cycling to a Specific Value

你可以通过给useAnimation产生的cycle方法传递index参数,来切换到某个特定的样式。

在上一个案例中,我们每次店家Frame标签都会在三个variants属性名中依次切换。但是在我们这里,我们想要让他在鼠标离开Frame元素时立即到“green”状态,那么我就在cycle方法那里给它传递了一个0,0就是“green”这个状态在使用useCycle这个方法时的下标值。

import * as React from "react"
import { Frame, useCycle } from "framer"

export function MyComponent() {
  const variants = {
    green: { background: "#1ea463" },
    yellow: { background: "#fecd45" },
    red: { background: "#de5347" },
  }

  const [current, cycle] = useCycle(
    "green",
    "yellow",
    "red"
  )

  return (
    <Frame
      variants={variants}
      animate={current}
      onTap={() => cycle()}
      onMouseLeave={() => cycle(0)}
    />
  )
}

PrevioususeAnimationNextuseMotionValue

Last updated 5 years ago

Was this helpful?