UGUI 整页滑动

实现的功能 ,点击左箭头,向左翻一页,点击右箭头,向右翻一页

鼠标可以按住滚动视图进行滑动,拖动结束时,会智能的停在合适的一张


这里写图片描述


这里写图片描述


    public void leftOrRight()  //向左还是向右
    {
        float zhi = scrollObj.horizontalScrollbar.value;
        if (zhi == 0)
        {
            scrollObj.horizontalScrollbar.value = 0;
        }
        else if(zhi>=jihe[jihe.Count-1])
        {
            scrollObj.horizontalScrollbar.value = jihe[jihe.Count - 1];
        }
        else
        {
            List<float> cache = new List<float>();

            float rightValue = 0;
            for (int jjj = 0; jjj < jihe.Count; jjj++)
            {
                if (jihe[jjj] > zhi)
                {
                    rightValue = jihe[jjj];
                    break;
                }
            }

            float leftValue = 0;
            List<float> huan = new List<float>();
            for (int jjj = 0; jjj < jihe.Count; jjj++)
            {
                if (jihe[jjj] < zhi)
                {
                    huan.Add(jihe[jjj]);

                }
            }
            leftValue = huan[huan.Count - 1];

            print(leftValue);
            print(rightValue);
            if (Mathf.Abs(leftValue - zhi) < Mathf.Abs(rightValue - zhi)) //向左滑动
            {
                scrollObj.horizontalScrollbar.value = leftValue;
            }
            else
            {
                scrollObj.horizontalScrollbar.value = rightValue;
            }
        }
    }

    public void zuo() //向左滑一张   左箭头对应这个方法
    {
        //先获取当前滑条的值
        float zhi = scrollObj.horizontalScrollbar.value;
       // print(zhi);
        zhi -= 0.01f;
        if (zhi <= 0)
        {

        }
        else
        {
            List<float> cache = new List<float>();


            for (int jjj = 0; jjj < jihe.Count; jjj++)
            {
                if (jihe[jjj] < zhi)
                {
                    print(jihe[jjj]);
                    cache.Add(jihe[jjj]);
                }
            }


            print(cache.Count);
            scrollObj.horizontalScrollbar.value = cache[cache.Count - 1];
        }

    }


    public void you() //向右滑一张  右箭头对应这个方法
    {
        //先获取当前滑条的值
        float zhi = scrollObj.horizontalScrollbar.value;
        print(zhi);
        zhi += 0.01f;
        if (zhi >=1)
        {

        }
        else
        {
            List<float> cache = new List<float>();


            for (int jjj = 0; jjj < jihe.Count; jjj++)
            {
                if (jihe[jjj] > zhi)
                {
                    print(jihe[jjj]);
                    cache.Add(jihe[jjj]);
                }
            }


            print(cache.Count);
            scrollObj.horizontalScrollbar.value = cache[0];
        }
    }


上面是一个脚本


下面这个脚本需要挂在 ScrollRect层级上


using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
   public delegate void  delegate01();

public class kkk : MonoBehaviour ,IEndDragHandler{

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        GameObject.Find("beijing").GetComponent<paizhao_beijing>().leftOrRight();
    }

}

这里写图片描述



这里写图片描述


我原本是想在实例化的Item上添加OnEndDrag,这个方法,但是这个方法,发现用不了,原因是有ScrollRect存在,就不能触发OnEndDrag,找了半天,最后发现在ScrollRect对象上挂着OnEndDrag就可以触发


FR:徐海涛(hunk Xu)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值