FLEX类似谷歌地图拖拽功能
要实现类似于谷歌地图拖拽功能,可以用s:Scroller标签来实现,代码如下:
mxml:
1 <s:Scroller width="100%" height="100%" interactionMode="touch" skinClass="skins.ScrollerSkin"> 2 <s:Group> 3 <component:Diagram id="diagram"> 4 </component:Diagram></s:Group> 5 </s:Scroller>
其中的component:Diagram为自己定义的组件,只需替换成你要放置的东西就OK.
皮肤文件 -->
1.ScrollerSkin.mxml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <s:SparkSkin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"> 3 4 <fx:Metadata> 5 <![CDATA[ 6 /** 7 * @copy spark.skins.spark.ApplicationSkin#hostComponent 8 */ 9 [HostComponent("spark.components.Scroller")] 10 ]]> 11 </fx:Metadata> 12 13 <fx:Script> 14 <![CDATA[ 15 import skins.*; 16 /** 17 * @private 18 */ 19 override public function beginHighlightBitmapCapture() : Boolean 20 { 21 var needUpdate:Boolean = super.beginHighlightBitmapCapture(); 22 23 // Draw an opaque rect that fill our entire skin. Our background 24 // is transparent, but we don't want focus/error skins to 25 // poke through. This is safe to do since we don't have any 26 // graphic elements as direct children. 27 graphics.beginFill(0); 28 graphics.drawRect(0, 0, width, height); 29 graphics.endFill(); 30 31 return needUpdate; 32 } 33 34 /** 35 * @private 36 */ 37 override public function endHighlightBitmapCapture() : Boolean 38 { 39 var needUpdate:Boolean = super.endHighlightBitmapCapture(); 40 41 // Clear the rect we drew in beginBitmapCapture(); 42 graphics.clear(); 43 return needUpdate; 44 } 45 ]]> 46 </fx:Script> 47 <s:VScrollBar id="verticalScrollBar" visible="false" skinClass="skins.VScrollerSkin"/> 48 <s:HScrollBar id="horizontalScrollBar" visible="false" skinClass="skins.HScrollBarSkin"/> 49 </s:SparkSkin>
2.VScrollerSkin.mxml:
<?xml version="1.0" encoding="utf-8"?> <s:SparkSkin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:fb="http://ns.adobe.com/flashbuilder/2009" minWidth="15" minHeight="15" alpha.disabled="0.5" alpha.inactive="0.5" > <fx:Script> <![CDATA[ import skins.ScrollerBarTrackSkin; import skins.ScrollerThumbSkin; ]]> </fx:Script> <fx:Metadata> <![CDATA[ /** * @copy spark.skins.spark.ApplicationSkin#hostComponent */ [HostComponent("spark.components.VScrollBar")] ]]> </fx:Metadata> <s:states> <s:State name="normal" /> <s:State name="disabled" /> <s:State name="inactive" /> </s:states> <s:Button id="track" top="1" bottom="1" width="10" focusEnabled="false" tabEnabled="false" skinClass="skins.ScrollerBarTrackSkin" /> <s:Button id="thumb" skinClass="skins.ScrollerThumbSkin" mouseEnabled="false" focusEnabled="false" visible.inactive="false" tabEnabled="false"/> </s:SparkSkin>
3.HScrollBarSkin.mxml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <s:SparkSkin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" 3 xmlns:fb="http://ns.adobe.com/flashbuilder/2009" minWidth="35" minHeight="15" 4 alpha.disabled="0.5" alpha.inactive="0.5"> 5 <fx:Script> 6 <![CDATA[ 7 8 import skins.ScrollerBarTrackSkin; 9 import skins.ScrollerThumbSkin; 10 ]]> 11 </fx:Script> 12 <fx:Metadata> 13 <![CDATA[ 14 /** 15 * @copy spark.skins.spark.ApplicationSkin#hostComponent 16 */ 17 [HostComponent("spark.components.HScrollBar")] 18 ]]> 19 </fx:Metadata> 20 <s:states> 21 <s:State name="normal" /> 22 <s:State name="disabled" /> 23 <s:State name="inactive" /> 24 </s:states> 25 <s:Button id="track" left="1" right="1" width="10" 26 focusEnabled="false" tabEnabled="false" 27 skinClass="skins.ScrollerBarTrackSkin" /> 28 <s:Button id="thumb" 29 focusEnabled="false" visible.inactive="false" tabEnabled="false" mouseEnabled="false" 30 skinClass="skins.ScrollerThumbSkin" /> 31 32 </s:SparkSkin>
4.ScrollerBarTrackSkin.mxml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <s:SparkButtonSkin xmlns:fx="http://ns.adobe.com/mxml/2009" 3 xmlns:s="library://ns.adobe.com/flex/spark" 4 xmlns:fb="http://ns.adobe.com/flashbuilder/2009" 5 minWidth="10" minHeight="10" 6 alpha.disabled="0.5"> 7 <fx:Metadata> 8 <![CDATA[ 9 /** 10 * @copy spark.skins.spark.ApplicationSkin#hostComponent 11 */ 12 [HostComponent("spark.components.Button")] 13 ]]> 14 </fx:Metadata> 15 <s:states> 16 <s:State name="up" /> 17 <s:State name="over" /> 18 <s:State name="down" /> 19 <s:State name="disabled" /> 20 </s:states> 21 <s:Rect top="0" bottom="0" left="0" right="0" minWidth="10" minHeight="10"> 22 </s:Rect> 23 </s:SparkButtonSkin>
5.ScrollerThumbSkin.mxml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <s:SparkButtonSkin xmlns:fx="http://ns.adobe.com/mxml/2009" 3 xmlns:s="library://ns.adobe.com/flex/spark" 4 xmlns:fb="http://ns.adobe.com/flashbuilder/2009" 5 minWidth="10" minHeight="10" 6 alpha.disabled="0.5"> 7 <fx:Metadata> 8 <![CDATA[ 9 /** 10 * @copy spark.skins.spark.ApplicationSkin#hostComponent 11 */ 12 [HostComponent("spark.components.Button")] 13 ]]> 14 </fx:Metadata> 15 <s:states> 16 <s:State name="up" /> 17 <s:State name="over" /> 18 <s:State name="down" /> 19 <s:State name="disabled" /> 20 </s:states> 21 <s:Rect top="1" left="1" right="1" bottom="1" radiusX="5" radiusY="5"> 22 <s:fill> 23 <s:SolidColor color="0xC0C0C0" alpha="0.8"/> 24 </s:fill> 25 </s:Rect> 26 </s:SparkButtonSkin>
效果见下图,当内容超过s:Scroller大小,并且只有拖拽的时候才会出现这个滚动条, 标签自带的拖到头部或尾部还会有弹性,就像谷歌地图的效果那样 O(∩_∩)O~: