AutoScrollManager Class: Scroll scrollbar when mouse is moving in a specified area and mouse button is down

konqside-bar Problem Summary: In a flex project, I need to insert an object to a object list which is derived from Container component. However, the list has fixed width and height, and the items of the list are too many to display one-time. So i have to scroll the list to the proper position then drag the object from a panel and insert it. I need a convenient way to complete this. I worte an AutoScrollManager utility to help myself.

Solution Summary: I add an event listener to the stage object of container’s systemManager object. It’s used to find the proper Container during the run-time because the container cannot trigger the MouseMove event while I am dragging the object from the panel. It is covered by what I am dragging. I have to find the proper container according to the Mouse coordinates. The function findContainer helps me a lot. Despite of this big problem, there is nothing hard to deal with.

<script type=text/javascript> </script>
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script> <script> window.google_render_ad(); </script>

Demo(DownloadDownload Full Project):

Press the left button of your mouse and do not release it. Move your mouse pointer inside the panel arbitrary. When the pointer moves around the texts, the scroll-bars scroll automatically.

Description: The AutoScrollManager provide two static functions to enable or disable auto-scrolling, addAutoScroll and removeAutoScroll, you should pass the target Container component to these functions. With the addAutoScroll function, there are another two parameters: scrollSpeed and bound. scrollSpeed determine the speed the scroll-bars scrolling. The bound is based on the model below:

clip_image002

viewMetrics is defined by the container self. Detailed info you will find in the flex SDK document.

* Returns an object that has four properties: <code>left</code>,

* <code>top</code>, <code>right</code>, and <code>bottom</code>.

* The value of each property equals the thickness of the chrome

* (visual elements) around the edge of the container.

Scroll bars appear when they are required.

Bound is defined by user. It is used to specify a particular area where the scroll bars scrolling automatically while the mouse is moving in it.

In this AutoScrollManger class , when the mouse is moving in the area of viewMetrics or Bound, the scroll bars scroll.

Codes:

Download: AutoScrolling.mxml
  1. <?xml version=”1.0encoding=”utf-8?>
  2. <mx:Application
  3. xmlns:mx=”http://www.adobe.com/2006/mxml
  4. layout=”absolute
  5. xmlns:local=”*”
  6. height=”200
  7. width=”200
  8. creationComplete=”initApp();”>
  9. <mx:XML id=”myDataxmlns="">
  10. <root>
  11. <item name=”asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl/>
  12. <item name=”asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl/>
  13. <item name=”asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl/>
  14. <item name=”asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl/>
  15. <item name=”asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl/>
  16. <item name=”asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl/>
  17. <item name=”asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl/>
  18. <item name=”asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl/>
  19. <item name=”asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl/>
  20. <item name=”asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl/>
  21. <item name=”asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl/>
  22. <item name=”asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl/>
  23. <item name=”asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl/>
  24. <item name=”asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl/>
  25. </root>
  26. </mx:XML>
  27. <mx:Script>
  28. <![CDATA[
  29. private function initApp():void
  30. {
  31. AutoScrollingManager.addAutoScroll(myPan);
  32. }
  33. ]]>
  34. </mx:Script>
  35. <mx:Panel id=”myPanwidth=”{application.width}” height=”{application.height}”>
  36. <mx:VBox width=”100%” height=”100%”>
  37. <mx:Repeater id=”myRepdataProvider=”{myData.item}”>
  38. <mx:Label text=”{myRep.currentItem.@name}”/>
  39. </mx:Repeater>
  40. </mx:VBox>
  41. </mx:Panel>
  42. </mx:Application>

Following is AutoScrollManager Class:

  1. import flash.display.Stage;
  2. import flash.events.Event;
  3. import flash.events.MouseEvent;
  4. import flash.geom.Point;
  5. import flash.utils.Dictionary;
  6. import mx.core.Container;
  7. import mx.core.EdgeMetrics;
  8. public class AutoScrollingManager
  9. {
  10. private static var managers:Dictionary = new Dictionary();
  11. public var scrollSpeed:int = 10;
  12. public var scrollFunction:Function = horizontalScroll;
  13. private var _bound:EdgeMetrics;
  14. public var bound:EdgeMetrics = new EdgeMetrics(20,20,20,20);
  15. public static function addAutoScroll(container:Container,scrollSpeed:int = 10,bound:EdgeMetrics = null):void
  16. {
  17. var manager:AutoScrollingManager = new AutoScrollingManager();
  18. manager.scrollSpeed = scrollSpeed;
  19. if(bound)manager.bound = bound;
  20. managers[container] = manager;
  21. container.systemManager.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
  22. container.systemManager.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
  23. }
  24. public static function removeAutoScroll(container:Container):void
  25. {
  26. container.systemManager.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
  27. container.systemManager.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
  28. if(managers[container])
  29. {
  30. container.systemManager.removeEventListener(Event.ENTER_FRAME,managers[container].scrollFunction);
  31. managers[container] = null;
  32. }
  33. }
  34. private static function onMouseMove(event:MouseEvent):void
  35. {
  36. var pt:Point = new Point(event.localX,event.localY);
  37. var container:Container = findContainer(pt,event.currentTarget.stage);
  38. if(!container)
  39. return;
  40. var manager:AutoScrollingManager = managers[container];
  41. if(!event.buttonDown)
  42. {
  43. container.systemManager.removeEventListener(Event.ENTER_FRAME,manager.scrollFunction);
  44. return;
  45. }
  46. var vm:EdgeMetrics = container.viewMetrics;
  47. var _bound:EdgeMetrics = new EdgeMetrics();
  48. _bound.bottom = manager.bound.bottom;
  49. _bound.left = manager.bound.left;
  50. _bound.right = manager.bound.right;
  51. _bound.top = manager.bound.top;
  52. if(container.horizontalScrollBar)_bound.bottom += container.horizontalScrollBar.height;
  53. if(container.verticalScrollBar)_bound.right += container.verticalScrollBar.width;
  54. if(vm.bottom!=0) _bound.bottom += vm.bottom;
  55. if(vm.left!=0) _bound.left += vm.left;
  56. if(vm.right!=0) _bound.right += vm.right;
  57. if(vm.top!=0) _bound.top += vm.top;
  58. pt = event.target.localToGlobal(pt);
  59. pt = container.globalToLocal(pt);
  60. var insideLeftTopCorner:Boolean;
  61. var insideLeftBottomCorner:Boolean ;
  62. var insideRightTopCorner:Boolean;
  63. var insideRightBottomCorner:Boolean ;
  64. insideLeftTopCorner = pt.x < _bound.left && pt.y < _bound.top;
  65. insideLeftBottomCorner = pt.x < _bound.left && pt.y > container.height - _bound.bottom;
  66. insideRightTopCorner = pt.x > container.width - _bound.right && pt.y < _bound.top;
  67. insideRightBottomCorner = pt.x > container.width - _bound.right&& pt.y > container.height - _bound.bottom;
  68. if(insideLeftTopCorner||insideLeftBottomCorner||insideRightTopCorner||insideRightBottomCorner)
  69. {
  70. container.systemManager.removeEventListener(Event.ENTER_FRAME,manager.scrollFunction);
  71. return;
  72. }
  73. if(container.horizontalScrollBar)
  74. {
  75. if(pt.y > container.height - container.horizontalScrollBar.height - vm.bottom)
  76. {
  77. return;
  78. }
  79. manager.scrollFunction = horizontalScroll;
  80. if(pt.x < _bound.left)
  81. {
  82. if( container.horizontalScrollPosition!=0)
  83. {
  84. container.systemManager.removeEventListener(Event.ENTER_FRAME,manager.scrollFunction);
  85. container.systemManager.addEventListener(Event.ENTER_FRAME,manager.scrollFunction);
  86. manager.scrollSpeed = Math.abs(manager.scrollSpeed)*-1;
  87. }
  88. }
  89. else if((pt.x > container.width-_bound.right && pt.x < container.width - (container.verticalScrollBar?container.verticalScrollBar.width:0) - vm.right) || pt.x > container.width - vm.right)
  90. {
  91. if(container.horizontalScrollPosition!=container.maxHorizontalScrollPosition)
  92. {
  93. container.systemManager.removeEventListener(Event.ENTER_FRAME,manager.scrollFunction);
  94. container.systemManager.addEventListener(Event.ENTER_FRAME,manager.scrollFunction);
  95. manager.scrollSpeed = Math.abs(manager.scrollSpeed);
  96. }
  97. }
  98. else
  99. {
  100. container.systemManager.removeEventListener(Event.ENTER_FRAME,manager.scrollFunction);
  101. }
  102. }
  103. if(container.verticalScrollBar)
  104. {
  105. if(pt.x > container.width - container.verticalScrollBar.width - vm.right)
  106. {
  107. return;
  108. }
  109. manager.scrollFunction = verticalScroll;
  110. if(pt.y < _bound.top)
  111. {
  112. if(container.verticalScrollPosition!=0)
  113. {
  114. container.systemManager.removeEventListener(Event.ENTER_FRAME,manager.scrollFunction);
  115. container.systemManager.addEventListener(Event.ENTER_FRAME,manager.scrollFunction);
  116. manager.scrollSpeed = Math.abs(manager.scrollSpeed)*-1;
  117. }
  118. }
  119. else if((pt.y > container.height - _bound.bottom && pt.y < container.height - (container.horizontalScrollBar?container.horizontalScrollBar.height:0) - vm.bottom) || pt.y > container.height - vm.bottom)
  120. {
  121. if(container.verticalScrollPosition!=container.maxVerticalScrollPosition)
  122. {
  123. container.systemManager.removeEventListener(Event.ENTER_FRAME,manager.scrollFunction);
  124. container.systemManager.addEventListener(Event.ENTER_FRAME,manager.scrollFunction);
  125. manager.scrollSpeed = Math.abs(manager.scrollSpeed);
  126. }
  127. }
  128. else
  129. {
  130. container.systemManager.removeEventListener(Event.ENTER_FRAME,manager.scrollFunction);
  131. }
  132. }
  133. }
  134. private static function onMouseUp(event:MouseEvent):void
  135. {
  136. event.currentTarget.removeEventListener(Event.ENTER_FRAME,horizontalScroll);
  137. event.currentTarget.removeEventListener(Event.ENTER_FRAME,verticalScroll);
  138. }
  139. private static function horizontalScroll(event:Event):void
  140. {
  141. var pt:Point = new Point(event.currentTarget.stage.mouseX,event.currentTarget.stage.mouseY);
  142. var container:Container = findContainer(pt,event.currentTarget.stage);
  143. if(!container)
  144. return;
  145. container.horizontalScrollPosition += managers[container].scrollSpeed;
  146. }
  147. private static function verticalScroll(event:Event):void
  148. {
  149. var pt:Point = new Point(event.currentTarget.stage.mouseX,event.currentTarget.stage.mouseY);
  150. var container:Container = findContainer(pt,event.currentTarget.stage);
  151. if(!container)
  152. return;
  153. container.verticalScrollPosition += managers[container].scrollSpeed;
  154. }
  155. private static function findContainer(pt:Point,stage:Stage):Container
  156. {
  157. var objects:Array = stage.getObjectsUnderPoint(pt);
  158. for each(var object:Object in objects)
  159. {
  160. var result:Object = doFind(object);
  161. if(result!=null)
  162. {
  163. return Container(result);
  164. }
  165. }
  166. return null;
  167. }
  168. private static function doFind(object:Object):Container
  169. {
  170. if(object.parent && object.parent is Container)
  171. {
  172. if(managers[object.parent] != null)
  173. {
  174. return object.parent;
  175. }
  176. else
  177. {
  178. return doFind(object.parent);
  179. }
  180. }
  181. return null;
  182. }
  183. }

引用:http://ntt.cc/2008/08/01/autoscrollmanager-class-scroll-scrollbar-when-mouse-is-moving-in-a-specified-area-and-mouse-button-is-down.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值