01 定义不同动作后的动画

onTapStart 时放大到 1.2 倍,onTap 时缩小回 1 倍(以带抖动的方式)。

2020-02-24 10.25.00.gif

  1. import * as React from "react"
  2. import { useAnimation, Override } from "framer"
  3. export function UseAnimation(): Override {
  4. const controls = useAnimation()
  5. return {
  6. // 调用定义的动画 controls
  7. animate: controls,
  8. onTapStart() {
  9. controls.start({
  10. scale: 1.2,
  11. })
  12. },
  13. onTap() {
  14. controls.start({
  15. scale: 1,
  16. transition: {
  17. type: "spring",
  18. stiffness: 500,
  19. },
  20. })
  21. },
  22. }
  23. }

02 Sequence 次序动画

给同一个组件设置多个动画,让他们按照次序进行。可设置每个动画的 transition 属性。下例中四个动画分别为:放大到 1.5 倍、缩小到 1 倍(第一个动画结束后 1 秒)、放大到两倍、缩小到 1 倍大小(带 spring 效果)。

2020-02-24 14.19.11.gif

  1. export function UseAnimationSequence(): Override {
  2. const controls = useAnimation()
  3. async function sequence() {
  4. await controls.start({
  5. scale: 1.5,
  6. })
  7. await controls.start({
  8. scale: 1,
  9. transition: {
  10. // 第一个动画开始 1 秒后开始这个动画
  11. delay: 1,
  12. },
  13. })
  14. await controls.start({
  15. scale: 2,
  16. })
  17. await controls.start({
  18. scale: 1,
  19. transition: {
  20. type: "spring",
  21. stiffness: 500,
  22. },
  23. })
  24. }
  25. return {
  26. animate: controls,
  27. onTap() {
  28. sequence()
  29. },
  30. }
  31. }

03 Dynamic Start 动态启动

给不同的组件的动画单独设置参数和时间次序等。

2020-02-24 16.14.07.gif

import * as React from "react"
import { useAnimation, Override } from "framer"

const delayTimeCameraContainer = 0.6
const delayTimeLens = 0.1
const upDistance = 20
export function cameraBackground(): Override {
    // 定义 controls 属性
    const controls3 = useAnimation()

    controls3.start(index => ({
        opacity: 1,
        scale: 1,
        transition: {
            delay: index * delayTimeCameraContainer,
            type: "spring",
            // 刚度
            stiffness: 280,
            // 阻尼
            damping: 20,
        },
    }))
    return {
        custom: 0,
        opacity: 1,
        scale: 0,
        animate: controls3,
    }
}

export function cameraLen1(): Override {
    // 定义 controls 属性
    const controls3 = useAnimation()

    controls3.start(index => ({
        opacity: 1,
        y: 0,
        transition: {
            delay: index * delayTimeLens + delayTimeCameraContainer,
        },
    }))
    return {
        custom: 1,
        opacity: 0,
        y: upDistance,
        animate: controls3,
    }
}

export function cameraLen2(): Override {
    // 定义 controls 属性
    const controls3 = useAnimation()

    controls3.start(index => ({
        opacity: 1,
        y: 0,
        transition: {
            delay: index * delayTimeLens + delayTimeCameraContainer,
        },
    }))
    return {
        custom: 2,
        opacity: 0,
        y: upDistance,
        animate: controls3,
    }
}

export function cameraLen3(): Override {
    // 定义 controls 属性
    const controls3 = useAnimation()

    controls3.start(index => ({
        opacity: 1,
        y: 0,
        transition: {
            delay: index * delayTimeLens + delayTimeCameraContainer,
        },
    }))
    return {
        custom: 3,
        opacity: 0,
        y: upDistance,
        animate: controls3,
    }
}

export function cameraLen4(): Override {
    // 定义 controls 属性
    const controls3 = useAnimation()

    controls3.start(index => ({
        opacity: 1,
        y: 0,
        transition: {
            delay: index * delayTimeLens + delayTimeCameraContainer,
        },
    }))
    return {
        custom: 4,
        opacity: 0,
        y: upDistance,
        animate: controls3,
    }
}

04 Stopping Animations 终止动画

使用 controls.stop() 来终止动画

image.png

image.png

image.png