Guest User

Untitled

a guest
Mar 18th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. using System;
  7. /// <summary>
  8. /// IBeginDragHandler开始拖动接口,IEndDragHandler结束拖动按钮
  9. /// </summary>
  10. public class ScrollDemo : MonoBehaviour,IBeginDragHandler,IEndDragHandler {
  11. [SerializeField]
  12. List<Toggle> toggles;
  13.  
  14. public ScrollRect scrollRect;
  15.  
  16. //目标位置
  17. public float targetPos=0;
  18.  
  19. //public InputField inputField;
  20.  
  21. //每页滚动区域定位
  22. public float[] posArr = new float[] { 0, 0.333f, 0.666f, 1f };
  23.  
  24. //是否在被拖动
  25. public bool isDrag = false;
  26.  
  27. public int curPageIndex = 0;
  28.  
  29. // Use this for initialization
  30. void Start () {
  31. scrollRect = this.GetComponent<ScrollRect>();
  32. //获取所有子对象中的所有Toggle组件
  33. toggles = new List<Toggle>();
  34. this.GetComponentsInChildren<Toggle>(toggles);
  35. //给每一个toggle添加点击事件
  36. foreach (Toggle item in toggles)
  37. {
  38. item.onValueChanged.AddListener(
  39. delegate (bool flag)
  40. {
  41. ToggleClicked(item, flag);
  42. }
  43. );
  44. }
  45. scrollRect.horizontalNormalizedPosition = 0;
  46. }
  47. /// <summary>
  48. /// 单选按钮被点击时触发监听
  49. /// </summary>
  50. /// <param name="toggle">被点击按钮</param>
  51. /// <param name="flag">是否被点击</param>
  52. void ToggleClicked(Toggle toggle,bool flag)
  53. {
  54. if (flag)
  55. {
  56.  
  57. //把名称中的p字符替换成空,只留下数字,然后转成int类型,做下表
  58. int index = int.Parse(toggle.name.Replace("p",""));
  59. curPageIndex = index;
  60. targetPos = posArr[index];
  61. //更新滚动区域
  62. //scrollRect.horizontalNormalizedPosition = posArr[index];
  63.  
  64.  
  65. }
  66. }
  67.  
  68. // Update is called once per frame
  69. void Update () {
  70. //判断是否在拖动过程
  71. if (!isDrag)
  72. {
  73. scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition, targetPos, Time.deltaTime * 5);
  74. }
  75.  
  76. }
  77.  
  78. //开始拖动函数
  79. public void OnBeginDrag(PointerEventData eventData)
  80. {
  81. isDrag = true;
  82. }
  83. //结束拖动函数
  84. public void OnEndDrag(PointerEventData eventData)
  85. {
  86. isDrag = false;
  87.  
  88. ////当前位置
  89. //float curPos = scrollRect.horizontalNormalizedPosition;
  90. ////误差
  91. //float minOffset = 0.5f;
  92. ////当前最近项索引
  93. //int posIndex = 0;
  94.  
  95. ////通过比较,得到最近页的位置
  96. //for (int i = 0; i < posArr.Length;i++)
  97. //{
  98. // //记录临时差
  99. // float tmp=Mathf.Abs(curPos - posArr[i]);
  100. // if (tmp<minOffset)
  101. // {
  102. // minOffset = tmp;
  103. // //存储下当前索引
  104. // posIndex = i;
  105. // }
  106. //}
  107.  
  108. ////更新目标位置
  109. //targetPos = posArr[posIndex];
  110. //toggles[posIndex].isOn = true;
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. /********
  119. * 第二中方法
  120. **************/
  121.  
  122. float curPos = scrollRect.horizontalNormalizedPosition;
  123. //根据当前和原来位置差,是正还是负,判断向左向右滑
  124. if (Mathf.Abs(curPos - targetPos) < 0.07f) return;
  125. if (curPos - targetPos > 0)
  126. {
  127. //如果向右滑,将页码+1
  128. curPageIndex = curPageIndex + 1 >= posArr.Length - 1 ? posArr.Length - 1 : curPageIndex + 1;
  129. }
  130. else
  131. {
  132. //向左滑,页码-1
  133. curPageIndex = curPageIndex - 1 <= 0 ? 0 : curPageIndex - 1;
  134. }
  135.  
  136. targetPos = posArr[curPageIndex];
  137. toggles[curPageIndex].isOn = true;
  138. }
  139. }
Add Comment
Please, Sign In to add comment