[摘要]官方提供的layer拖动样例,演示了蒙版、非蒙版、文本等形式layer拖动,使用方法只需要在ondown执行拖动动作即可。
代码说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<!-- 控制拖动的动作draglayer,原理是先判断对齐align方式,然后通过鼠标坐标计算移动到的新位置 --> <action name="draglayer"> if(%1 != dragging, copy(drag_currentx, x); copy(drag_currenty, y); copy(drag_stagex, mouse.stagex); copy(drag_stagey, mouse.stagey); set(drag_sx, +1); set(drag_sy, +1); if(align == righttop, set(drag_sx,-1); ); if(align == right, set(drag_sx,-1); ); if(align == rightbottom, set(drag_sx,-1); set(drag_sy,-1); ); if(align == bottom, set(drag_sy,-1); ); if(align == leftbottom, set(drag_sy,-1); ); draglayer(dragging); , if(pressed, sub(dx, mouse.stagex, drag_stagex); sub(dy, mouse.stagey, drag_stagey); mul(dx, drag_sx); mul(dy, drag_sy); add(x, drag_currentx, dx); add(y, drag_currenty, dy); delayedcall(0, draglayer(dragging) ); ); ); </action> <!-- 设置文字layer内文字上下滚动的动作 --> <action name="draglayer_text"> if(%1 != dragging, copy(drag_currenty, y); copy(drag_stagey, mouse.stagey); draglayer_text(dragging,%2); , if(pressed, sub(dy, mouse.stagey, drag_stagey); add(y1, drag_currenty, dy); add(y2, y1, pixelheight); sub(y2, %2); if(y1 GT 0, set(y1,0)); if(y2 LT 0, sub(y1,y2)); copy(y,y1); delayedcall(0, draglayer_text(dragging,%2) ); ); ); </action> <!-- 带蒙版的layer,ondown执行draglayer即可 --> <layer name="box1" type="container" align="lefttop" x="10" y="10" width="250" height="250" bgcolor="0x000000" bgalpha="0.5" bgcapture="true" maskchildren="true" ondown="draglayer();"> <layer name="box2" type="container" align="center" x="0" y="0" width="100" height="100" bgcolor="0x44AADD" bgalpha="1.0" bgcapture="true" ondown="draglayer();" /> </layer> <!-- 不带蒙版的layer,ondown执行draglayer即可 --> <layer name="box3" type="container" align="rightbottom" x="10" y="150" width="300" height="200" bgcolor="0x0033FF" bgalpha="0.5" bgcapture="true" maskchildren="false" ondown="draglayer();"> <layer name="box4" type="container" align="right" x="0" y="0" width="100" height="100" bgcolor="0x33FF33" bgalpha="0.7" bgcapture="true" ondown="draglayer();" /> </layer> <!-- 带蒙版的文字layer,ondown执行draglayer即可 --> <layer name="textbox" type="container" align="center" x="0" y="0" width="300" height="300" bgcolor="0x333333" bgalpha="0.7" bgcapture="true" ondown="draglayer();"> <layer name="title" url="%SWFPATH%/plugins/textfield.swf" children="false" enabled="false" align="top" y="5" width="100%" autoheight="true" border="false" background="false" css="text-align:center; color:#FFFFFF; font-family:Arial; font-weight:bold; font-size:14px;" html="Textbox" /> <layer name="textmask" type="container" align="lefttop" x="10" y="30" width="280" height="260" bgcolor="0xFFFFFF" bgalpha="1.0" maskchildren="true"> <!-- 文字层实现上下滚动,ondown执行draglayer_text即可,260是文本框高度,文字较度可以适当加大数值 --> <layer name="text" url="%SWFPATH%/plugins/textfield.swf" children="false" align="lefttop" x="0" y="0" width="100%" autoheight="true" border="false" background="false" css="text-align:left; color:#000000; font-family:Arial; font-size:15px;" html="data:testtext" ondown="draglayer_text(start,260);" /> </layer> </layer> |