OpenHarmony滑杆与其他控件的协同实践
一 核心协同方式
二 典型场景与实现要点
三 代码示例 ArkUI声明式
@Entry
@Component
struct SliderDemo {
@State speed: number = 5 // 旋转速度:1~10
@State scale: number = 1 // 缩放比例:0.5~2
@State angle: number = 0 // 当前旋转角度
@State showTip: boolean = false // 阈值提示
private timerId: number = -1
aboutToAppear() {
this.startRotation()
}
aboutToDisappear() {
clearInterval(this.timerId)
}
// 启动/更新旋转
private startRotation() {
clearInterval(this.timerId)
this.timerId = setInterval(() => {
this.angle = (this.angle + this.speed) % 360
}, 16) // ~60fps
}
build() {
Column({ space: 20 }) {
// 受滑杆控制的图片
Image($r('app.media.windmill'))
.width(150).height(150)
.rotate({ angle: this.angle })
.scale({ x: this.scale, y: this.scale })
// 速度滑杆
Text(`速度:${this.speed.toFixed(1)}`)
Slider({
value: this.speed,
min: 1,
max: 10,
step: 1
})
.showTips(true)
.onChange((value: number) => {
this.speed = value
this.startRotation()
})
// 缩放滑杆
Text(`缩放:${this.scale.toFixed(2)}`)
Slider({
value: this.scale,
min: 0.5,
max: 2,
step: 0.1
})
.showTips(true)
.onChange((value: number) => {
this.scale = value
})
// 阈值提示(条件渲染)
if (this.speed > 8) {
Text('⚠ 高速模式').fontColor(Color.Red)
}
// 表单提交校验
Button('提交')
.onClick(() => {
if (this.scale < 1) {
// 实际项目中可用弹窗/Toast替代
console.warn('缩放比例不能小于 1')
} else {
console.info('提交成功', { speed: this.speed, scale: this.scale })
}
})
}
.padding(20)
}
}
四 性能与体验建议
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。