目录

Spring Curve

In a damped spring system corresponding to a damped spring curve (spring curve for short), an object that deviates from the equilibrium position is forced to oscillate due to a reverse force generated by spring deformation; this oscillation is resisted by the damping force. Except for the special case where the damping is 0, the oscillation gradually decays in amplitude towards 0, and the resultant animation curve is natural and continuous.

An animation using the spring curve slows down toward the end until the velocity is 0, instead of stopping abruptly.

ArkUI provides four types of damped spring curve APIs:

The following shows a complete example and effect of spring curves. For details about how to connect gestures and animations using responsiveSpringMotion and springMotion, see Animation Smoothing.

The sample code and effect is as follows:

import { curves } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';

class Spring {
  public title: string;
  public subTitle: ResourceStr;
  public iCurve: ICurve;

  constructor(title: string, subTitle: ResourceStr, iCurve: ICurve) {
    this.title = title;
    this.iCurve = iCurve;
    this.subTitle = subTitle;
  }
}

// Spring component
@Component
struct Motion {
  @Prop dTranslate: number = 0;
  private title: string = '';
  private subTitle: ResourceStr = '';
  private iCurve: ICurve | undefined = undefined;

  build() {
    Column() {
      Circle()
        .translate({ y: this.dTranslate })
        .animation({ curve: this.iCurve, iterations: -1 })
        .foregroundColor('#317AF7')
        .width(30)
        .height(30)

      Column() {
        Text(this.title)
          .fontColor(Color.Black)
          .fontSize(10).height(30)
        Text(this.subTitle)
          .fontColor(0xcccccc)
          .fontSize(10).width(50)
      }
      .borderWidth({ top: 1 })
      .borderColor(0xf5f5f5)
      .width(80)
      .alignItems(HorizontalAlign.Center)
      .height(100)

    }
    .height(110)
    .margin({ bottom: 5 })
    .alignItems(HorizontalAlign.Center)
  }
}

@Entry
@Component
export struct SpringCurve {
  private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  @State dTranslate: number = 0;
  private springs: Spring[] = [
    // Replace $r('app.string.springCurve_text1') with the actual resource file. In this example, the value in the resource file is "Cycle 1; damping: 0.25."
    new Spring('springMotion', $r('app.string.springCurve_text1'), curves.springMotion(1, 0.25)),
    // Replace $r('app.string.springCurve_text2') with the actual resource file. In this example, the value in the resource file is "Responsive spring curve."
    new Spring('responsive' + '\n' + 'SpringMotion', $r('app.string.springCurve_text2'),
      curves.responsiveSpringMotion(1, 0.25)),
    // Replace $r('app.string.springCurve_text3') with the actual resource file. In this example, the value in the resource file is "Initial velocity: 10; quality: 1; stiffness: 228; damping: 30."
    new Spring('interpolating' + '\n' + 'Spring', $r('app.string.springCurve_text3'),
      curves.interpolatingSpring(10, 1, 228, 30)),
    // Replace $r('app.string.springCurve_text3') with the actual resource file. In this example, the value in the resource file is "Initial velocity: 10; quality: 1; stiffness: 228; damping: 30."
    new Spring('springCurve', $r('app.string.springCurve_text3'),
      curves.springCurve(10, 1, 228, 30))
  ];

  build() {
    Row() {
      ForEach(this.springs, (item: Spring) => {
        Motion({
          title: item.title,
          subTitle: item.subTitle,
          iCurve: item.iCurve,
          dTranslate: this.dTranslate
        })
      })
    }
    .justifyContent(FlexAlign.Center)
    .alignItems(VerticalAlign.Bottom)
    .width('100%')
    .height(437)
    .margin({ top: 20 })
    .onClick(() => {
      this.dTranslate = -50;
    })
  }
}