滚动类中的UIScrollBar组件时遇到麻烦
时间:2020-03-05 18:51:37 来源:igfitidea点击:
我正在尝试将UIScrollbar组件的实例添加到在加载某些XML之后生成的类的实例内的动态文本字段。滚动条组件已正确连接,因为滑块的大小随文本字段中内容的数量而变化,但是不会滚动。
这是代码:
function xmlLoaded(evt:Event):void { //do some stuff for(var i:int = 0; i < numProfiles; i++) { var thisProfile:profile = new profile(); thisProfile.alpha = 0; thisProfile.x = 0; thisProfile.y = 0; thisProfile.name = "profile" + i; profilecontainer.addChild(thisProfile); thisProfile.profiletextholder.profilename.htmlText = profiles[i].attribute("name"); thisProfile.profiletextholder.profiletext.htmlText = profiles[i].profiletext; //add scroll bar var vScrollBar:UIScrollBar = new UIScrollBar(); vScrollBar.direction = ScrollBarDirection.VERTICAL; vScrollBar.move(thisProfile.profiletextholder.profiletext.x + thisProfile.profiletextholder.profiletext.width, thisProfile.profiletextholder.profiletext.y); vScrollBar.height = thisProfile.profiletextholder.profiletext.height; vScrollBar.scrollTarget = thisProfile.profiletextholder.profiletext; vScrollBar.name = "scrollbar"; vScrollBar.update(); vScrollBar.visible = (thisProfile.profiletextholder.profiletext.maxScrollV > 1); thisProfile.profiletextholder.addChild(vScrollBar); //do some more stuff } }
我还尝试了在movieclip / class本身内的UIScrollBar组件进行尝试,但仍然无法正常工作。有任何想法吗?
解决方案
回答
我们是否尝试过将UI滚动条放到舞台上,在设计时将其绑定到文本字段,然后在加载事件期间调用update()?
过去,我在运行时动态创建UIScrollbars时有一些有趣的经验。
回答
在第一个示例中,我们是否尝试过将vScrollBar.update();
之后的声明addChild(vScollbar);
?
回答
一旦通过类似于以下内容的单独函数初始化了文本字段,就可以尝试添加滚动条:
private function assignScrollBar(tf:TextField, sb:UIScrollBar):void { trace("assigning scrollbar"); sb.move(tf.x + tf.width, tf.y); sb.setSize(15, tf.height); sb.direction = ScrollBarDirection.VERTICAL; sb.scrollTarget = tf; addChild(sb); sb.update(); }
这就是我目前的做法。