UGUI 整页滑动

来源:互联网 发布:淘宝买家金钻好处 编辑:程序博客网 时间:2024/05/17 08:58

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

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


这里写图片描述


这里写图片描述


    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)

原创粉丝点击