记录一下fgui的几种手势的代码实例
下面是以左上角为中心点的缩放
local worldMapView = {}
local this = worldMapView
local comps
local control
local doubleClickDist = 0 --双指初始距离
local singleClickpos = 0 --单指初始位置
local singleClickDist = 0 --单指移动位置差
local singleClickMapPos = nil --单击时map的位置
local mapPosSetting = {} --地图移动范围
local SwipeGestureEvent = nil --单指手势事件
local PinchGestureEvent = nil --双指手势事件
local curMapScale = 1 --当前地图放缩倍率
local mouseWheelRate = 0.05 --滚轮缩放比率
---------------------------------------------------------------------------
---------------------------------------------------------------------------
local SetMap = function(_, item)
local tempControl = {
item:GetChild("mapBtn01"),
item:GetChild("mapBtn02"),
item:GetChild("mapBtn03"),
item:GetChild("mapBtn04"),
item:GetChild("mapBtn05"),
item:GetChild("mapBtn06"),
item:GetChild("mapBtn07"),
}
for index, mapItem in pairs(tempControl) do
mapItem.enabled = (index == 1)
if (index == 1) then
this.ui:TouchClick(mapItem, function()
uiMgr.openUI(Constants.ViewName.mainHud)
this.ui:Close()
end)
end
end
end
local SetMapScale = function(scaleVal)
curMapScale = scaleVal
mapPosSetting.xMin = GRoot.inst.width - control.mapLayer.width * curMapScale
mapPosSetting.yMin = GRoot.inst.height - control.mapLayer.height * curMapScale
end
local MapLayerScale = function(add)
local scaleVal = control.mapLayer.scaleX + add
scaleVal = (scaleVal > mapPosSetting.scaleMax) and mapPosSetting.scaleMax or scaleVal
scaleVal = (scaleVal < mapPosSetting.scaleMin) and mapPosSetting.scaleMin or scaleVal
control.mapLayer:SetScale(scaleVal, scaleVal)
SetMapScale(scaleVal)
end
--双指事件move
local DoubleClickMoveEvent = function(context)
MapLayerScale(context.sender.delta)
end
--单指事件begin
local SingleClickBeginEvent = function(context)
singleClickpos = context.inputEvent.position
singleClickMapPos = control.mapLayer.position
end
--单指事件move
local SingleClickMoveEvent = function(context)
local tempPos = context.inputEvent.position - singleClickpos + singleClickMapPos
tempPos.x = (tempPos.x > mapPosSetting.xMax) and mapPosSetting.xMax or tempPos.x
tempPos.x = (tempPos.x < mapPosSetting.xMin) and mapPosSetting.xMin or tempPos.x
tempPos.y = (tempPos.y > mapPosSetting.yMax) and mapPosSetting.yMax or tempPos.y
tempPos.y = (tempPos.y < mapPosSetting.yMin) and mapPosSetting.yMin or tempPos.y
control.mapLayer.position = tempPos
control.mapLayer:InvalidateBatchingState()
end
---------------------------------------------------------------------------
--初始化节点
local InitControl = function()
control = {
backBtn = comps.backBtn,
mapLayer = comps.mapLayer,
}
mapPosSetting = {
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,
}
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)
end, function(context)
DoubleClickMoveEvent(context)
end, function()
end)
--滚轮事件
if UnityEngine.Application.platform == UnityEngine.RuntimePlatform.WindowsEditor or
UnityEngine.Application.platform == UnityEngine.RuntimePlatform.WindowsPlayer then
this.ui:OnMouseWheelEvent(control.mapLayer, function(context)
local delta = context.inputEvent.mouseWheelDelta
local val = (delta < 0) and mouseWheelRate or (mouseWheelRate * -1)
MapLayerScale(val)
end)
end
--测试
-- curMapScale = mapPosSetting.scaleMax
-- SetMapScale(curMapScale)
-- control.mapLayer:SetScale(mapPosSetting.scaleMax, mapPosSetting.scaleMax)
end
--初始化ui
local InitUI = function()
SetMap(nil, control.mapLayer)
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.comps
control = {}
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");
InitUI()
end
--OnExit方法名自动生成 请勿修改
function worldMapView.OnExit()
--关闭一个UI
this.RemoveAllEvent()
helper.logWarn("FairyGUI Lua => worldMapView OnExit")
end
--OnExitAll方法名自动生成 请勿修改
function worldMapView.OnExitAll()
--关闭所有UI
helper.logWarn("FairyGUI Lua => worldMapView OnExitAll")
end
--OnDestroy方法名自动生成 请勿修改
function worldMapView.OnDestroy()
helper.logWarn("FairyGUI Lua => worldMapView OnDestroy")
this.ui = nil
if (SwipeGestureEvent ~= nil) then
SwipeGestureEvent:Dispose()
end
if (PinchGestureEvent ~= nil) then
PinchGestureEvent:Dispose()
end
end
--方法名自动生成 请勿修改
return worldMapView
这个是手势的一些接口
-- 长按 手势
function BaseUI:LongPressGesture(gobject, begincb, movecb,endcb,params)
local longPress = LongPressGesture.New(gobject)
if params then
longPress.once = params.once or false
longPress.interval = params.interval or 0.3
--第一次 派发 事件的 触发间隔(长按 开始 响应的时间)
longPress.trigger = params.trigger or 1.5
--手指按住后,移动超出此半径范围则手势停止。
longPress.holdRangeRadius = params.holdRangeRadius or 50
longPress.itemIndex = params.itemIndex or 0
end
--开始的回调
if begincb then
longPress.onBegin:Add(begincb)
end
-- 每隔 多久 触发的回调
if movecb then
longPress.onAction:Add(movecb)
end
-- 结束的 回调
if endcb then
longPress.onEnd:Add(endcb)
end
return longPress
end
-- 单指 手势
function BaseUI:SwipeGesture(gobject, begincb, movecb, endcb)
local swipeGesture = SwipeGesture.New(gobject)
if begincb then
swipeGesture.onBegin:Add(begincb)
end
if movecb then
swipeGesture.onMove:Add(movecb)
end
if endcb then
swipeGesture.onEnd:Add(endcb)
end
return swipeGesture
end
-- 双指 手势
function BaseUI:PinchGesture(gobject, begincb, movecb, endcb)
local pinchGesture = PinchGesture.New(gobject)
if begincb then
pinchGesture.onBegin:Add(begincb)
end
if movecb then
pinchGesture.onAction:Add(movecb)
end
if endcb then
pinchGesture.onEnd:Add(endcb)
end
return pinchGesture
end
新增缩放以双指中心及鼠标点为中心的缩放逻辑
local worldMapView = {}
local this = worldMapView
local comps
local control
local 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) do
mapItem.enabled = (index <= curUnLockMapId)
if (index <= curUnLockMapId) then
this.ui:TouchClick(mapItem, function()
uiMgr.openUI(Constants.ViewName.mainHud)
this.ui:Close()
end)
end
end
end
local SetMapScale = function(scaleVal)
curMapScale = scaleVal
mapSetting.xMin = GRoot.inst.width - control.mapLayer.width * curMapScale
mapSetting.yMin = GRoot.inst.height - control.mapLayer.height * curMapScale
end
local SetMapPos = function(tempPos)
tempPos.x = (tempPos.x > mapSetting.xMax) and mapSetting.xMax or tempPos.x
tempPos.x = (tempPos.x < mapSetting.xMin) and mapSetting.xMin or tempPos.x
tempPos.y = (tempPos.y > mapSetting.yMax) and mapSetting.yMax or tempPos.y
tempPos.y = (tempPos.y < mapSetting.yMin) and mapSetting.yMin or tempPos.y
control.mapLayer.position = tempPos
end
local MapLayerScale = function(add, mousePos)
local scaleVal = control.mapLayer.scaleX + add
scaleVal = (scaleVal > mapSetting.scaleMax) and mapSetting.scaleMax or scaleVal
scaleVal = (scaleVal < mapSetting.scaleMin) and mapSetting.scaleMin or scaleVal
local ratio = (add > 0) and -1 or 1
local addWidth = math.abs(mapSetting.orginWidth * (scaleVal - curMapScale))
local addHeight = math.abs(mapSetting.orginHeight * (scaleVal - curMapScale))
local mapPos = control.mapLayer.position
mapPos.x = control.mapLayer.x + (mousePos.x / mapSetting.orginWidth) * ratio * addWidth
mapPos.y = control.mapLayer.y + (mousePos.y / mapSetting.orginHeight) * ratio * addHeight
SetMapScale(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.position
singleClickMapPos = control.mapLayer.position
end
--单指事件move
local SingleClickMoveEvent = function(context)
local tempPos = context.inputEvent.position - singleClickpos + singleClickMapPos
SetMapPos(tempPos)
control.mapLayer:InvalidateBatchingState()
end
---------------------------------------------------------------------------
--初始化节点
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 or
UnityEngine.Application.platform == UnityEngine.RuntimePlatform.WindowsPlayer then
this.ui:OnMouseWheelEvent(control.mapLayer, function(context)
local mousePos = control.mapLayer:GlobalToLocal(context.inputEvent.position)
local delta = context.inputEvent.mouseWheelDelta
local val = (delta < 0) and mouseWheelRate or (mouseWheelRate * -1)
MapLayerScale(val, mousePos)
end)
end
end
--初始化数据
local InitData = function()
curUnLockMapId = 1
end
--初始化ui
local InitUI = function()
SetMap()
--镜头锁定解锁地图
local tempPos = control.mapLayer.position
tempPos.x = GRoot.inst.width / 2 - control.mapItemList[curUnLockMapId].x * curMapScale
tempPos.y = GRoot.inst.height / 2 - control.mapItemList[curUnLockMapId].y * curMapScale
SetMapPos(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.comps
control = {}
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()
--关闭一个UI
this.RemoveAllEvent()
helper.logWarn("FairyGUI Lua => worldMapView OnExit")
end
--OnExitAll方法名自动生成 请勿修改
function worldMapView.OnExitAll()
--关闭所有UI
helper.logWarn("FairyGUI Lua => worldMapView OnExitAll")
end
--OnDestroy方法名自动生成 请勿修改
function worldMapView.OnDestroy()
helper.logWarn("FairyGUI Lua => worldMapView OnDestroy")
this.ui = nil
if (SwipeGestureEvent ~= nil) then
SwipeGestureEvent:Dispose()
end
if (PinchGestureEvent ~= nil) then
PinchGestureEvent:Dispose()
end
end
--方法名自动生成 请勿修改
return worldMapView