This commit is contained in:
lvyuankang 2024-12-30 13:21:26 +08:00
parent 40d5a9cdc0
commit 17106b34c3

View File

@ -0,0 +1,75 @@
@Component
export default struct imageBtn {
//状态变量unPressed用于控制按钮的状态
@State unPressed: boolean = true
btnWidth: string
btnHeight: string
imgSrc:Resource
opacityNum:number=1
@Styles customStyle(){
.width('100%')
.height('100%')
}
build() {
Column() {
Column() {
Stack() {
// Image($r('app.media.topB_back'))
Row() {
// 判断当前组件为放开状态
if (this.unPressed) {
// 插入Row组件配置过渡效果
Image(this.imgSrc)
// 水波纹扩散动画Row组件backgroundColor属性变更#fff -> #ccc系统插入动画过渡效果从组建的中心点开始放大scale{0,0}变更scale{1,1}
.transition({
type: TransitionType.Insert,
opacity: 0,
})
}
// 判断当前组件为按下状态
else if (!this.unPressed) {
// 插入Row组件配置过渡效果
Image(this.imgSrc)
.opacity(this.opacityNum)
.onAppear(() => {
// 水波纹聚拢动画Row组件backgroundColor属性变更#ccc -> #fff插入动画过渡效果scale{1,1}变化为scale{0,0}
animateTo({
duration: 500,
// 聚拢动画播放完成后需要衔接扩散动画此时Row组件backgroundColor属性变更#fff -> #ccc插入动画过渡效果scale{0,0}变化为scale{1,1}
onFinish: () => {
this.opacityNum=1
} },
() => {
this.opacityNum=0.2
})
})
}
// 其他状态
}
.justifyContent(FlexAlign.Center)
.alignItems(VerticalAlign.Center)
}
.customStyle()
}
.width(this.btnWidth)
.height(this.btnHeight)
}
// onTouch事件监听状态
.onTouch((event: TouchEvent) => {
// 当按钮按下时更新按钮的状态unPressedtrue -> false
if (event.type == TouchType.Down) {
animateTo({ duration: 400 }, () => {
this.unPressed = !this.unPressed
})
}
// 当按钮放开时更新按钮的状态unPressedfalse -> true
if (event.type == TouchType.Up) {
animateTo({ duration: 400 }, () => {
this.unPressed = !this.unPressed
})
}
})
}
}