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;
}
}
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;
}
}