Skip to content

Commit 2fed226

Browse files
committed
feat(picker-item): 增加齿轮拨动感
1 parent abca5d3 commit 2fed226

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/picker-item/picker-item.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export default class PickerItem extends SuperComponent {
6060
onTouchStart(event) {
6161
this.StartY = event.touches[0].clientY;
6262
this.StartOffset = this.data.offset;
63+
this.PrewIndex = this.data.curIndex; // 上一次Index,用于控制齿轮拨动效果
6364
this.setData({ duration: 0 });
6465
},
6566

@@ -69,9 +70,28 @@ export default class PickerItem extends SuperComponent {
6970
// touch偏移增量
7071
const touchDeltaY = event.touches[0].clientY - StartY;
7172
const deltaY = this.calculateViewDeltaY(touchDeltaY);
73+
const offset = range(StartOffset + deltaY, -(this.getCount() * itemHeight), 0);
74+
75+
// 产生拨动齿轮的震动
76+
if (wx.vibrateShort) {
77+
// 当前在第几个
78+
const index = range(Math.round(-offset / this.itemHeight), 0, this.getCount() - 1);
79+
80+
// 产生滚动 && 差不多到中间位置时
81+
if (this.PrewIndex !== index && Math.abs(-offset / this.itemHeight - index) < 0.1) {
82+
let _count = Math.abs(this.PrewIndex - index);
83+
// 如果一次性跨多了item
84+
while (_count > 0) {
85+
wx.vibrateShort({ type: 'light' }); // 执行震动效果
86+
_count -= 1;
87+
}
88+
89+
this.PrewIndex = index;
90+
}
91+
}
7292

7393
this.setData({
74-
offset: range(StartOffset + deltaY, -(this.getCount() * itemHeight), 0),
94+
offset: offset,
7595
duration: DefaultDuration,
7696
});
7797
},
@@ -80,11 +100,17 @@ export default class PickerItem extends SuperComponent {
80100
const { offset, labelAlias, valueAlias, columnIndex } = this.data;
81101
const { options } = this.properties;
82102

103+
// 调整偏移量
104+
const index = range(Math.round(-offset / this.itemHeight), 0, this.getCount() - 1);
105+
106+
// 执行震动
107+
if (index !== this.PrewIndex) {
108+
if (wx.vibrateShort) wx.vibrateShort({ type: 'light' });
109+
}
110+
83111
if (offset === this.StartOffset) {
84112
return;
85113
}
86-
// 调整偏移量
87-
const index = range(Math.round(-offset / this.itemHeight), 0, this.getCount() - 1);
88114
this.setData({
89115
curIndex: index,
90116
offset: -index * this.itemHeight,

0 commit comments

Comments
 (0)