NGUI中UIScrollView的翻页功能简单思路


代码:

/// <summary>滑动器 </summary>
    public UIScrollView scrollView;
    /// <summary>排序</summary>
    public UIGrid grid;
    /// <summary>最大偏移 </summary>
    private float maxOffset =  -1;

    // Use this for initialization
    void Start () {
        scrollView = this.transform.GetComponent<UIScrollView>();
        grid = this.transform.GetComponent<UIGrid>();
        scrollView.disableDragIfFits = false;               //当滑动器内容没有填满整个滑动页面时候也不关闭滑动功能
        scrollView.onDragFinished += TurnThePage;           //滑动完成时的事件
        //scrollView.onStoppedMoving += TurnThePage;
    }

    private void TurnThePage()
    {
        if (m_callBack == null)            return;
        //获取grid里面的布局子物体数量  而不是用transform.childCount
        int count = grid.GetChildList().Count;
        //计算需要在面板边界内约束的偏移量。
        Vector3 constraint = scrollView.panel.CalculateConstrainOffset(scrollView.bounds.min, scrollView.bounds.min);
        switch (scrollView.movement)
        {
            case UIScrollView.Movement.Horizontal:
                maxOffset = grid.cellWidth * (count - 1);
                if (constraint.x == 0f)
                {
                    Debug.Log("右一页");
                }
                else if (constraint.x >= maxOffset - grid.cellWidth)
                {
                    Debug.Log("坐一页");
                }
                break;
            case UIScrollView.Movement.Vertical:
                maxOffset = grid.cellHeight * (count - 1);
                if (constraint.y == 0f)
                {
                    Debug.Log("下一页");
                }
                else if (constraint.y >= maxOffset - grid.cellHeight)
                {
                    Debug.Log("上一页");
                }
                break;
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值