{"id":703,"date":"2020-04-22T19:16:15","date_gmt":"2020-04-22T11:16:15","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=703"},"modified":"2020-06-22T19:19:34","modified_gmt":"2020-06-22T11:19:34","slug":"%e8%87%aa%e5%ae%9a%e4%b9%89scrollview%e7%bb%84%e4%bb%b6%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=703","title":{"rendered":"\u81ea\u5b9a\u4e49Scrollview\u7ec4\u4ef6\u5f00\u53d1"},"content":{"rendered":"<div>\n<div>class CustomScrollerBar extends eui.Component {<\/div>\n<div>publicconstructor() {<\/div>\n<div>super();<\/div>\n<div>this.skinName = skins.CustomScrollerBarSkin;<\/div>\n<div>\/\/ this.addEventListener(egret.Event.RESIZE, this.viewResze, this);<\/div>\n<div>}<\/div>\n<div>publicscrollerBarGroup: eui.Group;<\/div>\n<div>publicscrollerBar: eui.Image;<\/div>\n<div>publicscrollerBg: eui.Image;<\/div>\n<div>publicmaskBg:eui.Image;<\/div>\n<div>privatescroller: eui.Scroller;<\/div>\n<div>privatescroller_bar_source: string;<\/div>\n<div>privatescroller_bg_source: string;<\/div>\n<div>privateui_loaded: boolean = false;<\/div>\n<div>protectedcreateChildren() {<\/div>\n<div>super.createChildren();<\/div>\n<div>\/\/ this.scrollerBar.height = this.scrollerBarGroup.height * 0.1;<\/div>\n<div>this.scrollerBarGroup.mask = this.maskBg;<\/div>\n<div>this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);<\/div>\n<div>this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);<\/div>\n<div>this.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);<\/div>\n<div>this.addEventListener(egret.TouchEvent.TOUCH_CANCEL, this.onTouchEnd, this);<\/div>\n<div>this.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.onTouchEnd, this);<\/div>\n<div>this.ui_loaded = true;<\/div>\n<div>if (this.scroller_bar_source != undefined)<\/div>\n<div>this.changeScrollerBarDisplayImage(this.scroller_bar_source, this.scroller_bg_source);<\/div>\n<div>}<\/div>\n<div>privatelastY: number = 0;<\/div>\n<div>privatetouch_id: number = -1;<\/div>\n<div>privateonTouchBegin(e: egret.TouchEvent) {<\/div>\n<div>if (!this.scrollerBar || this.touch_id != -1)<\/div>\n<div>return;<\/div>\n<div>if (this.scrollerBar.hitTestPoint(e.stageX, e.stageY)) {<\/div>\n<div>this.touch_id = e.touchPointID;<\/div>\n<div>this.lastY = e.stageY;<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div>privatechangeScrollBarHeight() {<\/div>\n<div>\/\/scroller bug \u9700\u8981\u5ef6\u8fdf\u83b7\u53d6 contentHeight, \u5426\u5219\u53d6\u5230\u7684\u503c\u4e3a0<\/div>\n<div>setTimeout(() =&gt; {<\/div>\n<div>if (!this.scroller || !this.scrollerBar)<\/div>\n<div>return;<\/div>\n<div>\/\/\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6<\/div>\n<div>if (this.scroller.viewport.contentHeight == 0)<\/div>\n<div>return;<\/div>\n<div>this.scrollerBarGroup.height = this.height;<\/div>\n<div>letper = this.scroller.height \/ this.scroller.viewport.contentHeight;<\/div>\n<div>this.scrollerBar.height = this.scrollerBarGroup.height * per;<\/div>\n<div>}, 100);<\/div>\n<div>\/\/ console.log(&#8220;change scroller bar height &#8220;, this.scrollerBar.height, this.scrollerBarGroup.height, per, this.scroller.height, this.scroller.viewport.getBounds().height);<\/div>\n<div>}<\/div>\n<div>privateonTouchMove(e: egret.TouchEvent) {<\/div>\n<div>if (this.touch_id != e.touchPointID)<\/div>\n<div>return;<\/div>\n<div>lety = e.stageY;<\/div>\n<div>this.scrollerBar.y += (y &#8211; this.lastY);<\/div>\n<div>this.lastY = y;<\/div>\n<div>letmaxY = this.scrollerBarGroup.height &#8211; this.scrollerBar.height;<\/div>\n<div>if (this.scrollerBar.y &gt; maxY) {<\/div>\n<div>this.scrollerBar.y = maxY;<\/div>\n<div>}<\/div>\n<div>if (this.scrollerBar.y &lt; 0) {<\/div>\n<div>this.scrollerBar.y = 0;<\/div>\n<div>}<\/div>\n<div>this.changeScrollerV();<\/div>\n<div>}<\/div>\n<div>privateonTouchEnd(e: egret.TouchEvent) {<\/div>\n<div>this.touch_id = -1;<\/div>\n<div>this.lastY = 0;<\/div>\n<div>}<\/div>\n<div>privateonScrollerChange() {<\/div>\n<div>if (!this.scroller)<\/div>\n<div>return;<\/div>\n<div>letlength = this.scrollerBarGroup.height &#8211; this.scrollerBar.height;<\/div>\n<div>letpercent = this.scroller.viewport.scrollV \/ (this.scroller.viewport.contentHeight &#8211; this.scroller.height);<\/div>\n<div>\/\/ if (percent &gt; 1)<\/div>\n<div>\/\/ percent = 1;<\/div>\n<div>\/\/ if (percent &lt; 0)<\/div>\n<div>\/\/ percent = 0;<\/div>\n<div>lety = length * percent;<\/div>\n<div>this.scrollerBar.y = y;<\/div>\n<div>}<\/div>\n<div>privatechangeScrollerV() {<\/div>\n<div>letper = this.scrollerBar.y \/ (this.scrollerBarGroup.height &#8211; this.scrollerBar.height);<\/div>\n<div>if (per &lt; 0)<\/div>\n<div>per = 0;<\/div>\n<div>this.scroller.viewport.scrollV = per * (this.scroller.viewport.contentHeight &#8211; this.scroller.height);<\/div>\n<div>}<\/div>\n<div>\/**<\/div>\n<div>* \u7ed1\u5b9ascroller<\/div>\n<div>*\/<\/div>\n<div>publicbindScroller(scroller: eui.Scroller) {<\/div>\n<div>this.scroller = scroller;<\/div>\n<div>this.scroller.addEventListener(egret.Event.CHANGE, this.onScrollerChange, this);<\/div>\n<div>this.scroller.verticalScrollBar.alpha = 0;<\/div>\n<div>this.scroller.horizontalScrollBar.alpha = 0;<\/div>\n<div>this.changeScrollBarHeight();<\/div>\n<div>}<\/div>\n<div>\/**<\/div>\n<div>* \u66ff\u6362ui\u56fe\u7247<\/div>\n<div>*\/<\/div>\n<div>publicchangeScrollerBarDisplayImage(scrollerBar: string, scrollerBg: string) {<\/div>\n<div>this.scroller_bar_source = scrollerBar;<\/div>\n<div>this.scroller_bg_source = scrollerBg;<\/div>\n<div>if (!this.ui_loaded)<\/div>\n<div>return;<\/div>\n<div>lettexture = RES.getRes(this.scroller_bg_source);<\/div>\n<div>this.scrollerBg.texture = texture;<\/div>\n<div>this.maskBg.texture = texture;<\/div>\n<div>this.scrollerBar.texture = RES.getRes(this.scroller_bar_source);<\/div>\n<div>this.width = this.scrollerBg.width;<\/div>\n<div>this.scrollerBarGroup.width = this.width;<\/div>\n<div>this.scrollerBg.height = this.scrollerBarGroup.height;<\/div>\n<div>this.maskBg.height = this.scrollerBarGroup.height;<\/div>\n<div>this.changeScrollBarHeight();<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>class CustomScrollerBar extends eui.Component { publicc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":469,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[15],"_links":{"self":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/703"}],"collection":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=703"}],"version-history":[{"count":1,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/703\/revisions"}],"predecessor-version":[{"id":704,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/703\/revisions\/704"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/media\/469"}],"wp:attachment":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=703"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}