добавил скрол по touchEvent по скролл бару
This commit is contained in:
parent
6eb39e0791
commit
d7c00a42f2
39
assets/Scripts/ux/ScrollSync.ts
Normal file
39
assets/Scripts/ux/ScrollSync.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { _decorator, Component, ScrollView, ScrollBar, UITransform, EventTouch, Vec3, clamp } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('ScrollHandler')
|
||||
export class ScrollHandler extends Component {
|
||||
@property(ScrollView) scrollView: ScrollView = null!;
|
||||
@property(ScrollBar) scrollBar: ScrollBar = null!;
|
||||
|
||||
onLoad() {
|
||||
// Перед тем как подписываться, убедимся, что handle у ScrollBar не пустой
|
||||
const handleSprite = this.scrollBar.handle;
|
||||
if (!handleSprite) {
|
||||
console.error('У ScrollBar отсутствует handle–спрайт!');
|
||||
return;
|
||||
}
|
||||
handleSprite.node.on('touch-move', this.onDrag, this);
|
||||
}
|
||||
|
||||
private onDrag(evt: EventTouch) {
|
||||
const handleSprite = this.scrollBar.handle;
|
||||
if (!handleSprite) {
|
||||
return;
|
||||
}
|
||||
const barNode = this.scrollBar.node;
|
||||
const ui = barNode.getComponent(UITransform)!;
|
||||
const worldPos = evt.getLocation();
|
||||
// переводим координаты в локальную систему полосы
|
||||
const local = ui.convertToNodeSpaceAR(new Vec3(worldPos.x, worldPos.y, 0));
|
||||
|
||||
const H = ui.height;
|
||||
const handleH = handleSprite.node.getComponent(UITransform)!.height;
|
||||
// рассчитываем долю прокрутки
|
||||
let pct = (local.y + H/2 - handleH/2) / (H - handleH);
|
||||
pct = clamp(pct, 0, 1);
|
||||
|
||||
// и прокручиваем ScrollView: scrollToPercentVertical(1 - pct)
|
||||
this.scrollView.scrollToPercentVertical(1 - pct, 0);
|
||||
}
|
||||
}
|
||||
9
assets/Scripts/ux/ScrollSync.ts.meta
Normal file
9
assets/Scripts/ux/ScrollSync.ts.meta
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "3a213e52-0450-4f60-bdb7-30ab6bd6a528",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user