HarmonyOS 通过 animateTo讲解角度动画效果

本文 我们依旧来说动画

这次 我们来说角度

我们先写一个这样的代码模板

typescript 复制代码
@Entry
@Component
struct Index {

  build() {
    Column({space: 30}) {
      Text("修改元素尺寸")
        .fontSize(38)
        .margin({top:188})
      Image("https://img2.baidu.com/it/u=1814561676,2470063876&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500")
        .width(200)
        .height(150)

      Button("执行").onClick((event: ClickEvent) => {
        animateTo({
          duration: 3000,
          curve: Curve.Linear,
          //delay: 2000,
          iterations: 3,
          playMode: PlayMode.Alternate,
          onFinish: (()=>{
            console.log("动画结束");
          })
        },() => {
        })
      })
  }
    .width('100%')
    .height('100%')
  }
}

这里 我们放了一个 Image 图片组件 里面是个网图地址

然后 我们还是用按钮定义了一个点击事假 里面用了 animateTo 动画 不过目前 没有写动画是要干嘛的

这里 我们就可以在点击事件中修改角度属性

我们先给 Image 组件加上属性rotate

typescript 复制代码
Image("https://img2.baidu.com/it/u=1814561676,2470063876&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500")
.width(200)
.height(150)
.rotate({
    angle: 30,
})

rotate 就是设置元素旋转的属性

angle 设置中心点偏转的角度 我们给 30

可以看到 我们的图片 中心就转了一个幅度

然后 我们将他打 rotate 改成

typescript 复制代码
.rotate({
	angle: 30,
	x: 1
})

这里 我们给 x 1的意思是 我们要按x轴去偏转

这样 我们图片偏转的角度 明显就变了

x轴 为面对我们这个上下正方向 90度为一个半圆 那么 我们来一个 80度

我们整个图片 也成了快躺平的一个状态

180度 整个圆 它就整个反过来了

我们改成y轴

typescript 复制代码
rotate({
	angle: 40,
	 y: 1
})

然后是 z轴 她就说 我们最开始看到的这个平面旋转的效果

那么 这里其实 我们控制 angle 就OK了

这里还有个点 例如 我们x y z都打开

那 它是三轴都发生旋转

如果你不想让某个轴转 给0就好了

例如

typescript 复制代码
rotate({
    angle: 40,
	x: 0,
	y: 0,
	z: 1
})

这样 就只有为1的z轴转了

代码最上面 我们写一个状态变量 number 数字类型

来控制 angle 角度属性 然后 默认给0

typescript 复制代码
@State rotateAngle:number = 0;

然后 用 rotateAngle 绑定 旋转角度属性

typescript 复制代码
rotate({
	angle: this.rotateAngle,
	 x: 0,
	 y: 0,
	 z: 1
})

然后 我们将 下面 Button按钮的代码修改如下

typescript 复制代码
Button("执行").onClick((event: ClickEvent) => {
	animateTo({
	 duration: 3000,
	 curve: Curve.Linear,
	 //delay: 2000,
	 iterations: 3,
	 playMode: PlayMode.Alternate,
	 onFinish: (()=>{
	   console.log("动画结束");
	 })
	},() => {
	 this.rotateAngle = 180
	})
})

只需要在箭头函数中 修改 rotateAngle 的值 之前的第一参数配置都是一样的

然后 我们点击按钮 整个动画 他就转起来了

相关推荐
Georgewu3 小时前
【HarmonyOS 6】 The target can not be empty. check the build.profile,json5 file of
harmonyos
Georgewu3 小时前
【HarmonyOS 6】Install Failed: error: failed to install bundle.code:9568322
harmonyos
爱笑的眼睛115 小时前
HarmonyOS 应用开发新范式:深入剖析 Stage 模型与 ArkTS 状态管理
华为·harmonyos
爱笑的眼睛116 小时前
深入浅出 HarmonyOS ArkUI 3.0:基于声明式开发范式与高级状态管理构建高性能应用
华为·harmonyos
电手8 小时前
时隔4年麒麟重新登场!华为这8.8英寸新「手机」给我看麻了
华为·智能手机
程序员潘Sir9 小时前
鸿蒙应用开发从入门到实战(一):鸿蒙应用开发概述
harmonyos
敲代码的鱼哇12 小时前
跳转原生系统设置插件 支持安卓/iOS/鸿蒙UTS组件
android·ios·harmonyos
在下历飞雨13 小时前
Kuikly基础之状态管理与数据绑定:让“孤寡”计数器动起来
ios·harmonyos
在下历飞雨13 小时前
Kuikly基础之Kuikly DSL基础组件实战:构建青蛙主界面
ios·harmonyos
HarmonyOS小助手14 小时前
HEIF:更高质量、更小体积,开启 HarmonyOS 图像新体验
harmonyos·鸿蒙·鸿蒙生态