FGUI手势案例代码
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                FGUI手势案例代码
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                記錄一下fgui的幾種手勢的代碼實例
 下面是以左上角為中心點的縮放
這個是手勢的一些接口
-- 長按 手勢 function BaseUI:LongPressGesture(gobject, begincb, movecb,endcb,params)local longPress = LongPressGesture.New(gobject)if params thenlongPress.once = params.once or falselongPress.interval = params.interval or 0.3--第一次 派發(fā) 事件的 觸發(fā)間隔(長按 開始 響應的時間)longPress.trigger = params.trigger or 1.5--手指按住后,移動超出此半徑范圍則手勢停止。longPress.holdRangeRadius = params.holdRangeRadius or 50longPress.itemIndex = params.itemIndex or 0end--開始的回調(diào)if begincb thenlongPress.onBegin:Add(begincb)end-- 每隔 多久 觸發(fā)的回調(diào)if movecb thenlongPress.onAction:Add(movecb)end-- 結(jié)束的 回調(diào)if endcb thenlongPress.onEnd:Add(endcb) endreturn longPressend-- 單指 手勢 function BaseUI:SwipeGesture(gobject, begincb, movecb, endcb)local swipeGesture = SwipeGesture.New(gobject)if begincb thenswipeGesture.onBegin:Add(begincb)endif movecb thenswipeGesture.onMove:Add(movecb)endif endcb thenswipeGesture.onEnd:Add(endcb) endreturn swipeGestureend-- 雙指 手勢 function BaseUI:PinchGesture(gobject, begincb, movecb, endcb)local pinchGesture = PinchGesture.New(gobject)if begincb thenpinchGesture.onBegin:Add(begincb)endif movecb thenpinchGesture.onAction:Add(movecb)endif endcb thenpinchGesture.onEnd:Add(endcb) endreturn pinchGestureend新增縮放以雙指中心及鼠標點為中心的縮放邏輯
local worldMapView = {} local this = worldMapViewlocal comps local controllocal doubleClickDist = 0 --雙指初始距離 local singleClickpos = 0 --單指初始位置 local singleClickDist = 0 --單指移動位置差 local singleClickMapPos = nil --單擊時map的位置 local mapSetting = {} --地圖移動范圍 local SwipeGestureEvent = nil --單指手勢事件 local PinchGestureEvent = nil --雙指手勢事件 local curMapScale = 1 --當前地圖放縮倍率 local mouseWheelRate = 0.01 --滾輪縮放比率 local doubleClickCenterPos = nil --雙指事件兩指中心位置 local curUnLockMapId = 1 --當前解鎖的最新地圖Id------------------------------------------------------------------------------------------------------------------------------------------------------ local SetMap = function()for index, mapItem in pairs(control.mapItemList) domapItem.enabled = (index <= curUnLockMapId)if (index <= curUnLockMapId) thenthis.ui:TouchClick(mapItem, function()uiMgr.openUI(Constants.ViewName.mainHud)this.ui:Close()end)endend endlocal SetMapScale = function(scaleVal)curMapScale = scaleValmapSetting.xMin = GRoot.inst.width - control.mapLayer.width * curMapScalemapSetting.yMin = GRoot.inst.height - control.mapLayer.height * curMapScale endlocal SetMapPos = function(tempPos)tempPos.x = (tempPos.x > mapSetting.xMax) and mapSetting.xMax or tempPos.xtempPos.x = (tempPos.x < mapSetting.xMin) and mapSetting.xMin or tempPos.xtempPos.y = (tempPos.y > mapSetting.yMax) and mapSetting.yMax or tempPos.ytempPos.y = (tempPos.y < mapSetting.yMin) and mapSetting.yMin or tempPos.ycontrol.mapLayer.position = tempPos endlocal MapLayerScale = function(add, mousePos)local scaleVal = control.mapLayer.scaleX + addscaleVal = (scaleVal > mapSetting.scaleMax) and mapSetting.scaleMax or scaleValscaleVal = (scaleVal < mapSetting.scaleMin) and mapSetting.scaleMin or scaleVallocal ratio = (add > 0) and -1 or 1local addWidth = math.abs(mapSetting.orginWidth * (scaleVal - curMapScale))local addHeight = math.abs(mapSetting.orginHeight * (scaleVal - curMapScale))local mapPos = control.mapLayer.positionmapPos.x = control.mapLayer.x + (mousePos.x / mapSetting.orginWidth) * ratio * addWidthmapPos.y = control.mapLayer.y + (mousePos.y / mapSetting.orginHeight) * ratio * addHeightSetMapScale(scaleVal)SetMapPos(mapPos)control.mapLayer:SetScale(scaleVal, scaleVal) end--雙指事件begin local DoubleClickBeginEvent = function(context)local pos1 = control.mapLayer:GlobalToLocal(Stage.inst:GetTouchPosition(0))local pos2 = control.mapLayer:GlobalToLocal(Stage.inst:GetTouchPosition(1))doubleClickCenterPos = (pos1 + pos2) / 2 end--雙指事件move local DoubleClickMoveEvent = function(context)MapLayerScale(context.sender.delta, doubleClickCenterPos) end--單指事件begin local SingleClickBeginEvent = function(context)singleClickpos = context.inputEvent.positionsingleClickMapPos = control.mapLayer.position end--單指事件move local SingleClickMoveEvent = function(context)local tempPos = context.inputEvent.position - singleClickpos + singleClickMapPosSetMapPos(tempPos)control.mapLayer:InvalidateBatchingState() end--------------------------------------------------------------------------- --初始化節(jié)點 local InitControl = function()control = {backBtn = comps.backBtn,mapLayer = comps.mapLayer,mapItemList = {comps.mapLayer:GetChild("mapBtn01"),comps.mapLayer:GetChild("mapBtn02"),comps.mapLayer:GetChild("mapBtn03"),comps.mapLayer:GetChild("mapBtn04"),comps.mapLayer:GetChild("mapBtn05"),comps.mapLayer:GetChild("mapBtn06"),comps.mapLayer:GetChild("mapBtn07"),},}mapSetting = {xMin = GRoot.inst.width - control.mapLayer.width * curMapScale,xMax = 0,yMin = GRoot.inst.height - control.mapLayer.height * curMapScale,yMax = 0,scaleMax = control.mapLayer.width / 2500,scaleMin = GRoot.inst.height / control.mapLayer.height,orginWidth = control.mapLayer.width,orginHeight = control.mapLayer.height,}SetMapScale(curMapScale)end--注冊點擊事件 local BindBtnEvent = function()--部隊配置this.ui:TouchClick(control.backBtn, function()uiMgr.openUI(Constants.ViewName.options)this.ui:Close()end)--單指手勢SwipeGestureEvent = this.ui:SwipeGesture(control.mapLayer, function(context)SingleClickBeginEvent(context)end, function(context)SingleClickMoveEvent(context)end, function()end)--雙指手勢PinchGestureEvent = this.ui:PinchGesture(GRoot.inst, function(context)DoubleClickBeginEvent(context)end, function(context)DoubleClickMoveEvent(context)end, function()end)--滾輪事件if UnityEngine.Application.platform == UnityEngine.RuntimePlatform.WindowsEditor orUnityEngine.Application.platform == UnityEngine.RuntimePlatform.WindowsPlayer thenthis.ui:OnMouseWheelEvent(control.mapLayer, function(context)local mousePos = control.mapLayer:GlobalToLocal(context.inputEvent.position)local delta = context.inputEvent.mouseWheelDeltalocal val = (delta < 0) and mouseWheelRate or (mouseWheelRate * -1)MapLayerScale(val, mousePos)end)endend--初始化數(shù)據(jù) local InitData = function()curUnLockMapId = 1 end--初始化ui local InitUI = function()SetMap()--鏡頭鎖定解鎖地圖local tempPos = control.mapLayer.positiontempPos.x = GRoot.inst.width / 2 - control.mapItemList[curUnLockMapId].x * curMapScaletempPos.y = GRoot.inst.height / 2 - control.mapItemList[curUnLockMapId].y * curMapScaleSetMapPos(tempPos) end--------------------------------------------------------------------------- --onUpdate function this.onUpdate() end--OnInit方法名自動生成 請勿修改 function worldMapView.OnInit(root)--自動生成 不可刪除this.ui = BaseUI:New(root)helper.logWarn("FairyGUI Lua => worldMapView onInit");comps = this.ui.compscontrol = {}InitControl()BindBtnEvent() end--注冊事件方法名自動生成 請勿修改 function worldMapView.OnRegisterEvent()helper.logWarn("FairyGUI Lua => worldMapView onRegisterEvent"); end --RemoveAllEvent方法名自動生成 請勿修改 function worldMapView.RemoveAllEvent()--移除事件綁定helper.logWarn("FairyGUI Lua => worldMapView removeAllEvent"); end--OnEnter方法名自動生成 請勿修改 function worldMapView.OnEnter(...)local args = {...}-----------------------------獲取并初始化組件this.OnRegisterEvent()helper.logWarn("FairyGUI Lua => worldMapView onEnter");InitData()InitUI() end --OnExit方法名自動生成 請勿修改 function worldMapView.OnExit()--關閉一個UIthis.RemoveAllEvent()helper.logWarn("FairyGUI Lua => worldMapView OnExit") end--OnExitAll方法名自動生成 請勿修改 function worldMapView.OnExitAll()--關閉所有UIhelper.logWarn("FairyGUI Lua => worldMapView OnExitAll") end--OnDestroy方法名自動生成 請勿修改 function worldMapView.OnDestroy()helper.logWarn("FairyGUI Lua => worldMapView OnDestroy")this.ui = nilif (SwipeGestureEvent ~= nil) thenSwipeGestureEvent:Dispose()endif (PinchGestureEvent ~= nil) thenPinchGestureEvent:Dispose()end end--方法名自動生成 請勿修改 return worldMapView總結(jié)
以上是生活随笔為你收集整理的FGUI手势案例代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: jupyter notebook
- 下一篇: HDU 5037 Frog(2014年北
