--拖住功能 DragViewMgr = DragViewMgr or BaseClass() local DragViewMgr = DragViewMgr DragViewMgr.TouchType = { TouchDown = 1, TouchMove = 2, TouchUp = 3, } function DragViewMgr:__init() self.base_file = false self.layout_file = false self.begin_callback = false self.draging_callback = false self.end_callback = false self.item_class = false self.drag_cache_item = false self.start_drag = false self.draging = false self.old_pos = false end function DragViewMgr:Init(info) self.base_file = info.base_file self.layout_file = info.layout_file self.begin_callback = info.begin_callback self.draging_callback = info.draging_callback self.end_callback = info.end_callback self.item_class = info.item_class self.item_parent = info.item_parent or MainUIController:GetInstance().GoodsFlyView.transform end function DragViewMgr:__delete() if self.drag_cache_item then self.drag_cache_item:DeleteMe() self.drag_cache_item = nil end end function DragViewMgr:Start() self.start_drag = true self.draging = false Runner.Instance:AddRunObj(self, 1) end function DragViewMgr:Stop() Runner.Instance:RemoveRunObj(self) self.start_drag = false self.draging = false self.old_pos = false end function DragViewMgr:OnDragBegin(pos) if not pos then return end if not self.drag_cache_item then self.drag_cache_item = self:CreatDragItem() end if not self.drag_cache_item then print(">>>>>>>>>>>> 拖拽物体没有创建成功") return end if self.begin_callback then self.begin_callback(self.drag_cache_item) end self.drag_cache_item:SetVisible(true) local w = ScreenWidth local h = ScreenHeight pos.x = w / 2 + pos.x pos.y = h / 2 + pos.y self.drag_cache_item:SetPosition(pos.x, pos.y) end function DragViewMgr:CreatDragItem() local item = nil if self.item_class ~= nil then item = self.item_class.New(self.item_parent) self.drag_cache_item = item end return item end function DragViewMgr:OnDragIng(pos) local w = ScreenWidth local h = ScreenHeight pos.x = w / 2 + pos.x pos.y = h / 2 + pos.y if self.drag_cache_item then self.drag_cache_item:SetPosition(pos.x, pos.y) end if self.draging_callback then self.draging_callback(self.drag_cache_item, pos) end end function DragViewMgr:OnDragEnd(pos) Runner.Instance:RemoveRunObj(self) if self.end_callback then self.end_callback(self.drag_cache_item, pos) end if self.drag_cache_item then self.drag_cache_item:SetVisible(false) end end function DragViewMgr:Update() if not self.start_drag then return end local touch_type, pos = self:GetDragTouchTypeAndPos() if not pos or not touch_type then if self.draging then self:Stop() self:OnDragEnd(self.old_pos) end end if touch_type == DragViewMgr.TouchType.TouchDown or touch_type == DragViewMgr.TouchType.TouchMove then if not self.draging then self:OnDragBegin(pos) self.draging = true self.old_pos = pos return end self.old_pos = pos self:OnDragIng(pos) else self:OnDragEnd(pos) self:Stop() end end function DragViewMgr:GetDragTouchTypeAndPos() local touch_type = nil local pos = nil if Input.touchCount > 0 then local touch = Input.GetTouch(0) if touch then pos = touch.position pos.x = pos.x / ScreenConvertRatio - ScreenWidth*0.5 pos.y = pos.y / ScreenConvertRatio - ScreenHeight*0.5 if touch.phase == TouchPhase.Began then touch_type = DragViewMgr.TouchType.TouchDown elseif touch.phase == TouchPhase.Moved then touch_type = DragViewMgr.TouchType.TouchMove elseif touch.phase == TouchPhase.Ended then touch_type = DragViewMgr.TouchType.TouchUp end end else if Input.GetMouseButtonDown(0) then -- print(">>>>>>>>>>>> GetMouseButtonDown") pos = Vector2(0,0) pos.x = Input.mousePosition.x / ScreenConvertRatio - ScreenWidth*0.5 pos.y = Input.mousePosition.y / ScreenConvertRatio - ScreenHeight*0.5 touch_type = DragViewMgr.TouchType.TouchDown elseif Input.GetMouseButtonUp(0) then -- print(">>>>>>>>>>>> GetMouseButtonUp") pos = Vector2(0,0) pos.x = Input.mousePosition.x / ScreenConvertRatio - ScreenWidth*0.5 pos.y = Input.mousePosition.y / ScreenConvertRatio - ScreenHeight*0.5 touch_type = DragViewMgr.TouchType.TouchUp elseif Input.GetMouseButton(0) then --print(">>>>>>>>>>>> GetMouseButton") pos = Vector2(0,0) pos.x = Input.mousePosition.x / ScreenConvertRatio - ScreenWidth*0.5 pos.y = Input.mousePosition.y / ScreenConvertRatio - ScreenHeight*0.5 touch_type = DragViewMgr.TouchType.TouchMove end end return touch_type, pos end