cocos2d-js裡面解決滾動容器沒有滾動條的問題
阿新 • • 發佈:2019-01-23
1.在編輯器裡面編輯好滾動容器,命名為“ScrollView”;在編輯器裡面編輯好滾動條,命名為“Slider”;是由於滾動容器不顯示內建滾動條的緣故,我們只能自己去做一個;
2.程式碼裡面載入滾動容器和滾動條,並添對應加偵聽事件:
this.ScrollView = ccui.helper.seekWidgetByName(layer.node, "ScrollView"); this.ScrollView .addEventListener(this.onScrollViewEvent,this); this.SlidView = ccui.helper.seekWidgetByName(layer.node, "slidView"); this.SlidView.addEventListener(this.onSlidViewEvent,this);
3.實現用滾動容器的位置去設定滾動條,用滾動條的位置去設定滾動容器目前滾動的座標:
onScrollViewEvent:function(sender,type){ switch (type) { case ccui.ScrollView.EVENT_SCROLLING : var percent = -sender.getInnerContainer().y / (sender.getInnerContainer().height - sender.height) * 100; this.SlidView.setPercent(percent); } break; default: break; } }, onSlidViewEvent:function(sender, type) { if (type != ccui.Slider.EVENT_PERCENT_CHANGED) return ; var percent = sender.getPercent(); var tempHei = this.ScrollView.getInnerContainer().height - this.ScrollView.height; var tempPercent = - ( percent * tempHei / 100 ); this.ScrollView.getInnerContainer().y = tempPercent; },