手游商城翻页、滑动(源码)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


/// <summary>
///  IBeginDragHandler开始拖动接口, IEndDragHandler 拖动结束接口
/// </summary>
public class ScrollPage : MonoBehaviour,IBeginDragHandler,IEndDragHandler {


    //每一页滚动区域的定位坐标
    public float[] posArr = new float[] { 0, 0.3333f, 0.666f, 1 };


    //目标位置
    public float targetPos = 0;


    //获取滚动组件
    public ScrollRect scrollRect;
    
    //获取输入文本
    public InputField inputField;


    //是否在被拖动
    public bool isDrag = false;


    List<Toggle> toggles;


    public int curPageIndex = 0;


    // Use this for initialization
    void Start () {
        scrollRect = this.GetComponent<ScrollRect>();
        //获取输入文本
        inputField = this.transform.parent.parent.Find("InputField").GetComponent<InputField>();


        //获取所有子对象中的所有Toggle组件,并转存到List中
       toggles=new List<Toggle>();
        this.GetComponentsInChildren<Toggle>(toggles);


        //为每一个toggle添加点击监听
        foreach (var item in toggles)
        {
            item.onValueChanged.AddListener(delegate(bool flag) {


                ToggleClicked(item, flag);


            });
        }
        scrollRect.horizontalNormalizedPosition = 0;
    }


    //单选按钮值改变了
    void ToggleClicked(Toggle item,bool flag)
    {
        if (flag)
        {
            //把名称中的p字符替换成空,只留下数字,然后转成int类型作位置下标
            int index = int.Parse(item.name.Replace("p", ""));
            Debug.Log(posArr.Length);
            curPageIndex = index;
            //更新目标位置
            targetPos = posArr[index];
        }
    }

// Update is called once per frame
void Update () {
        Debug.Log(scrollRect.horizontalNormalizedPosition);


        //判断是否在拖动过程中
        if (!isDrag)
        {
            //利用插值进行缓动
            scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition, targetPos, Time.deltaTime * 5);
        }
}


    //开始拖动
    public void OnBeginDrag(PointerEventData eventData)
    {
        isDrag = true;
    }


    public void OnEndDrag(PointerEventData eventData)
    {
        isDrag = false;




        /*****************
         * 第1中方法
         * ************


        
        //当前位置
        float curPos = scrollRect.horizontalNormalizedPosition;
        //误差
        float minOffset = 1;
        //最近项索引
        int posIndex = 0;


        //通过比较,得到最近的页位置
        for (int i = 0; i < posArr.Length; i++)
        {
            //记录临时差
            float tmp = Mathf.Abs(curPos - posArr[i]);
            if (tmp < minOffset)
            {
                minOffset = tmp;
                //存储下当前索引
                posIndex = i;
            }
        }
        //更新目标位置
        targetPos = posArr[posIndex];


        //更新Toggle选择
        toggles[posIndex].isOn = true;
       */




        /********
         * 第二中方法
         **************/


        float curPos = scrollRect.horizontalNormalizedPosition;
        //根据当前和原来位置差,是正还是负,判断向左向右滑
        if (Mathf.Abs(curPos - targetPos) < 0.07f) return;
        if (curPos - targetPos > 0)
        {
            //如果向右滑,将页码+1
            curPageIndex = curPageIndex + 1 >= posArr.Length - 1 ? posArr.Length - 1 : curPageIndex + 1;
        }else
        {
            //向左滑,页码-1
            curPageIndex = curPageIndex - 1 <= 0 ? 0 : curPageIndex - 1;
        }


        targetPos = posArr[curPageIndex];
        toggles[curPageIndex].isOn = true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值