HarmonyOS鸿蒙Next中如何实现提交表单错误选项抖动效果?
HarmonyOS鸿蒙Next中如何实现提交表单错误选项抖动效果?
3 回复
效果

思路
点击按钮时,控制输入框的scale属性,通过显示动画animateTo 运动曲线模拟物理弹簧效果
完整代码
import curves from "@ohos.curves"
@Entry
@Component
export struct Index {
@State textPhone: string = ''
@State doScale: ScaleOptions = { x: 1, y: 1 }
startAnimation() {
animateTo({
duration: 800,
// 应用`curves.springCurve`生成的Spring Curve动画曲线,模拟物理弹簧效果
curve: curves.springCurve(0, 10, 80, 10),
iterations: 1,
}, () => {
this.doScale = { x: 1.1, y: 1.1 };
})
this.doScale = { x: 1, y: 1 };
}
build() {
Column() {
TextInput({ text: $$this.textPhone, placeholder: '请输入手机号' })
.margin({ top: 30 })
// .padding({ left: 0 })
.width('658lpx')
.height('96lpx')
.scale(this.doScale)
.backgroundColor(Color.White)
.type(InputType.PhoneNumber)
.maxLength(13)
.placeholderColor("#CBCBCB")
.fontColor("#2E2E2E")
.fontSize('36lpx')
.caretColor('#FF1919')
.borderRadius(40)
Button('动画测试').margin({ top: 30 }).onClick(() => {
this.startAnimation()
})
}.width('100%').height('100%')
.backgroundColor("#f5f5f5")
}
}
更多关于HarmonyOS鸿蒙Next中如何实现提交表单错误选项抖动效果?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS Next中,实现表单错误选项抖动效果可使用ArkUI的animation动画。通过animateTo方法结合translate变换,快速改变组件位置。示例代码为错误组件设置关键帧动画,在X轴方向小幅度来回移动,模拟抖动。动画时长通常设为200-500毫秒,循环次数2-3次。需在表单验证失败时触发此动画。


