1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
const {ccclass, property, requireComponent, menu} = cc._decorator; @ccclass @requireComponent(cc.Slider) @menu("组件库/ScrollBarEnhance") export class ScrollBarEnhance extends cc.Component { @property({ tooltip: "对应的(ScrollView)滑动列表", type: cc.ScrollView }) private scrollView: cc.ScrollView = null!; @property({ type: [cc.Component.EventHandler], tooltip: CC_DEV && "滑动事件改变事件", }) private renderEvent: cc.Component.EventHandler[] = [];
private vSlider: cc.Slider = null!;
private static creatEventHandler(target: cc.Node, component: string, handler: string) { const e = new cc.Component.EventHandler(); e.target = target; e.component = component; e.handler = handler; return e; }
public refresh(p: number) { let progress, slider = this.vSlider || this.node.getComponent(cc.Slider); progress = Math.min(p, 1); progress = Math.max(p, 0); slider.progress = progress; this.onSliderEvent(slider, ""); console.log("slider.progress----:" + JSON.stringify(slider.progress)); }
protected start() { this.addScrollChildIndex(); }
private addScrollChildIndex() { const scroll = this.scrollView; this.vSlider = this.node.getComponent(cc.Slider); let className = cc.js.getClassName(this); this.vSlider.slideEvents.push(ScrollBarEnhance.creatEventHandler(this.node, className, 'onSliderEvent')); scroll.scrollEvents.push(ScrollBarEnhance.creatEventHandler(this.node, className, 'onScrollEvent')); this.scrollMoveEvent(); }
private onScrollEvent(scroll: cc.ScrollView, eventType: cc.ScrollView.EventType, customEventData: string) { switch (eventType) { case cc.ScrollView.EventType.SCROLL_BEGAN: break; case cc.ScrollView.EventType.SCROLL_ENDED: break; case cc.ScrollView.EventType.SCROLLING: this.scrollMoveEvent(); break; } }
private scrollMoveEvent() { let vertical = this.scrollView.vertical, maxScrollOffset, getScrollOffset; if (vertical) { maxScrollOffset = this.scrollView.getMaxScrollOffset().y; getScrollOffset = this.scrollView.getScrollOffset().y; } else { maxScrollOffset = this.scrollView.getMaxScrollOffset().x; getScrollOffset = this.scrollView.getScrollOffset().x * (-1); } if (getScrollOffset / maxScrollOffset > 0 && getScrollOffset / maxScrollOffset < 1) { this.vSlider.progress = getScrollOffset / maxScrollOffset; } else if (getScrollOffset / maxScrollOffset <= 0) { this.vSlider.progress = 0; } else { this.vSlider.progress = 1; } console.log("scrollMoveEvent", this.vSlider.progress); if (this.renderEvent && this.renderEvent.length) { cc.Component.EventHandler.emitEvents(this.renderEvent, this.vSlider.progress); } }
private onSliderEvent(slider, customEventData: string) { let vertical = this.scrollView.vertical, maxScrollOffsetX = 0, maxScrollOffsetY = 0; if (vertical) { maxScrollOffsetX = this.scrollView.getScrollOffset().x; maxScrollOffsetY = this.scrollView.getMaxScrollOffset().y * slider.progress; } else { maxScrollOffsetX = this.scrollView.getMaxScrollOffset().x * slider.progress; maxScrollOffsetY = this.scrollView.getScrollOffset().y; } this.scrollView.scrollToOffset(cc.v2(maxScrollOffsetX, maxScrollOffsetY), 0.05); } }
|