Lua 无线滑动 新

---@class UIScrollView:UIBaseComponent
local UIScrollView = BaseClass("UIScrollView", UIBaseContainer)
local base = UIBaseContainer

local GridLayoutGroup = CS.UnityEngine.UI.GridLayoutGroup
local TextAnchor = CS.UnityEngine.TextAnchor

ScrollType =
{
    Horizontal = "Horizontal" --竖直滑动
    ,
    Vertical = "Vertical"     --水平滑动
}
-- 创建
function UIScrollView:OnCreate()
    base.OnCreate(self)
    self.Scrollrect = nil
    self.content = nil
    self.layout = nil
    self.scrollType = nil
    self.fixedCount = nil
    self.itemPrefab = nil
    self.totalCount = 0 --总的数据数量
    self.dataList = {}  --数据实体列表
    self.objectList = {}
    self.headIndex = 0  --头下标
    self.tailIndex = 0  --尾下标
    self.firstItemAnchoredPos = Vector2.one
    self.__onrefresh = nil
end

-- 设置刷新回调
function UIScrollView:SetOnRefresh(...)
    self.__onrefresh = BindCallback(...)
end

-- 创建

-- 滑动类型
-- 生成数量
-- 预制体
-- 当前实例的View
-- item 需要添加的脚本
function UIScrollView:Init(scrollType, fixedCount, itemPrefab, view, target)
    self.Scrollrect = self.transform:GetComponentInParent(typeof(CS.UnityEngine.UI.ScrollRect))
    self.content = self.Scrollrect.content
    self.content.localPosition = Vector2(0, 0, 0)
    self.layout = self.content:GetComponent(typeof(CS.UnityEngine.UI.GridLayoutGroup))
    self.layout.enabled = true
    self.scrollType = scrollType
    self.fixedCount = fixedCount
    self.itemPrefab = itemPrefab
    self.view = view

    --设置布局
    self:SetLayout()

    --设置头下标和尾下标
    self.headIndex = 0
    self.tailIndex = fixedCount - 1


    --实例化Item
    self:InitItem(target)

    --设置Content大小
    self:SetContentSize()
    --得到第一个Item的锚点位置
    self:GetFirstItemAnchoredPos()

    self.__onmove = function(vec2)
        self:OnScroll(vec2)
    end
    self.Scrollrect.onValueChanged:AddListener(self.__onmove)
end

-- <summary>
-- 设置布局
-- </summary>
function UIScrollView:SetLayout()
    self.layout.startCorner = GridLayoutGroup.Corner.UpperLeft
    self.layout.startAxis = GridLayoutGroup.Axis.Horizontal
    self.layout.childAlignment = TextAnchor.UpperLeft
    self.layout.constraintCount = 1

    if (self.scrollType == ScrollType.Vertical) then
        self.Scrollrect.horizontal = false
        self.Scrollrect.vertical = true
        self.layout.constraint = GridLayoutGroup.Constraint.FixedColumnCount
    end
end

-- <summary>
-- 设置Content大小
-- </summary>
function UIScrollView:SetContentSize()
    self.content.sizeDelta = Vector2
        (
            self.layout.cellSize.y,
            self.layout.padding.top + self.layout.padding.bottom +
            self.totalCount * (self.layout.cellSize.y + self.layout.spacing.y) -
            self.layout.spacing.y
        )
end

-- <summary>
--得到第一个数据的锚点位置
-- </summary>
function UIScrollView:GetFirstItemAnchoredPos()
    self.firstItemAnchoredPos = Vector2(0, -self.layout.padding.top - self.layout.cellSize.y / 2)
end

-- <summary>
--设置显示
-- </summary>
function UIScrollView:SetShow(item, index)
    item.gameObject.name = index
    if self.__onrefresh ~= nil then
        self.__onrefresh(item, index)
    end
    -- item:SetData(self.itemdata[index], self, type)
end

-- <summary>
--实例化制定数量 itme 并且赋值
-- </summary>
function UIScrollView:InitItem(component)
    local child_count = self.content.transform.childCount
    for i = 0, child_count - 1 do
        local passitem = self.dataList[i + 1]
        if passitem == nil then
            local obj = self.content.transform:GetChild(i).gameObject
            obj.name = i
            table.insert(self.objectList, obj)
            passitem = self.view:AddComponent(component, obj)
            table.insert(self.dataList, passitem)
        end
        self:SetPos(passitem, i)
        self:SetShow(passitem, i)
    end
end

-- <summary>
--实例化制定数量 itme 并且赋值
-- </summary>
function UIScrollView:GetdataList()
    return self.dataList
end

-- <summary>
--指定位置
-- </summary>
function UIScrollView:SetNormalizedPosition(v2)
    if self.Scrollrect then
        self.Scrollrect.normalizedPosition = v2
    end
end

-- <summary>
--动画移动指定位置
-- </summary>
-- 位置
-- 时间
-- 曲线 (默认匀速)
function UIScrollView:SetDoNormalizedPosition(v2, time, ease)
    if not ease then
        ease = Ease.Linear
    end
    if self.Scrollrect then
        self.Scrollrect:DONormalizedPos(v2, time):SetEase(ease)
    end
end

-- <summary>
--重置item 位置
-- </summary>
function UIScrollView:SetPos(trans, index)
    if self.Scrollrect then
        if (self.scrollType == ScrollType.Vertical) then
            local y = nil
            if index == 0 then
                y = -self.layout.padding.top
            else
                y = -self.layout.padding.top - index * (self.layout.cellSize.y + -self.layout.spacing.y)
            end
            trans.localPosition = Vector2(0, y)
            -- Logger.Log(trans.anchoredPosition)
        end
    end
end

-- <summary>
--设定滑动物体的数量
-- </summary>
function UIScrollView:SetTotalCount(count)
    self.totalCount = count
end

-- <summary>
--滑动中
-- </summary>
function UIScrollView:OnScroll(v)
    if self.Scrollrect == nil then
        return
    end
    if (self.scrollType == ScrollType.Vertical) then
        while self.content.anchoredPosition.y >= self.layout.padding.top + (self.headIndex + 1) * (self.layout.cellSize.y + self.layout.spacing.y)
            and self.tailIndex ~= self.totalCount - 1 do
            self.layout.enabled = false
            local item = self.dataList[1]
            local rectitem = self.dataList[1].rectTransform
            table.remove(self.dataList, 1)
            table.insert(self.dataList, item)
            self.headIndex = self.headIndex + 1
            self.tailIndex = self.tailIndex + 1
            self:SetPos(rectitem, self.tailIndex)
            self:SetShow(item, self.tailIndex)
            print(self.headIndex .. "---------:---------" .. self.tailIndex)
        end

        while self.content.anchoredPosition.y <= self.layout.padding.top + self.headIndex * (self.layout.cellSize.y + self.layout.spacing.y)
            and self.headIndex ~= 0 do
            self.layout.enabled = false
            local item = self.dataList[#self.dataList]
            local rectitem = self.dataList[#self.dataList].rectTransform
            table.remove(self.dataList, #self.dataList)
            table.insert(self.dataList, 1, item)
            self.headIndex = self.headIndex - 1
            self.tailIndex = self.tailIndex - 1
            self:SetPos(rectitem, self.headIndex)
            self:SetShow(item, self.headIndex)
            print(self.headIndex .. "---------:---------" .. self.tailIndex)
        end
    elseif (self.scrollType == ScrollType.Horizontal) then
        while self.content.anchoredPosition.x <= -self.layout.padding.left - (self.headIndex + 1) * (self.layout.cellSize.x + self.layout.spacing.x)
            and self.tailIndex ~= self.totalCount - 1 do
            self.layout.enabled = false
            local item = self.dataList[1]
            local rectitem = self.dataList[1].rectTransform
            table.remove(self.dataList, 1)
            table.insert(self.dataList, item)
            self.headIndex = self.headIndex + 1
            self.tailIndex = self.tailIndex + 1
            self:SetPos(rectitem, self.tailIndex)
            self:SetShow(item, self.tailIndex)
            print(self.headIndex .. "---------:---------" .. self.tailIndex)
        end

        while self.content.anchoredPosition.y >= self.layout.padding.left - self.headIndex * (self.layout.cellSize.x + self.layout.spacing.x)
            and self.headIndex ~= 0 do
            self.layout.enabled = false
            local item = self.dataList[#self.dataList]
            local rectitem = self.dataList[#self.dataList].rectTransform
            table.remove(self.dataList, #self.dataList)
            table.insert(self.dataList, 1, item)
            self.headIndex = self.headIndex - 1
            self.tailIndex = self.tailIndex - 1
            self:SetPos(rectitem, self.headIndex)
            self:SetShow(item, self.headIndex)
            print(self.headIndex .. "---------:---------" .. self.tailIndex)
        end
    end
end

function UIScrollView:Refresh()
    if self.Scrollrect then
        if self.__onmove ~= nil then
            self.Scrollrect.onValueChanged:RemoveListener(self.__onmove)
        end
        self.Scrollrect.normalizedPosition = Vector2(0, 0)
        self.Scrollrect = nil
        self.content.sizeDelta = Vector2(0, 0)
        self.content.localPosition = Vector2(0, 0, 0)
        self.content = nil
        self.layout.enabled = true
        self.layout = nil
        self.scrollType = nil
        self.fixedCount = nil
        self.itemPrefab = nil
        self.totalCount = nil --总的数据数量
        self.headIndex = nil  --头下标
        self.tailIndex = nil  --尾下标
        local index = 0
        local index2 = 0
        for i = 1, #self.dataList, 1 do
            for j = 1, #self.dataList - i, 1 do
                index = tonumber(self.dataList[j].__name)
                index2 = tonumber(self.dataList[j + 1].__name)
                if index > index2 then
                    local temp = self.dataList[j]
                    self.dataList[j] = self.dataList[j + 1]
                    self.dataList[j + 1] = temp
                end
            end
        end
    end
end

function UIScrollView:OnDestroy()
    if self.Scrollrect then
        if self.__onmove ~= nil then
            self.Scrollrect.onValueChanged:RemoveListener(self.__onmove)
        end
        self.Scrollrect.normalizedPosition = Vector2(0, 0)
        self.Scrollrect = nil
        self.content.sizeDelta = Vector2(0, 0)
        self.content.localPosition = Vector2(0, 0, 0)
        self.content = nil
        self.layout.enabled = true
        self.layout = nil
        self.scrollType = nil
        self.fixedCount = nil
        self.itemPrefab = nil
        self.totalCount = nil --总的数据数量
        self.dataList = {}    --数据实体列表
        self.headIndex = nil  --头下标
        self.tailIndex = nil  --尾下标
    end
    base.OnDestroy(self)
end

return UIScrollView

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值