using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
//添加了两个拖拽开始拖拽结束接口,对滑动进行检测
public class LevelButtonScrollRect : MonoBehaviou,IBeginDragHandler,IEndDragHandler
{
private ScrollRect scrollRect;
private float targetHorizontalPosition=0;
private bool isDraging = false;
// Use this for initialization
void Start ()
{
scrollRect = GetComponent<ScrollRect>();
}
// Update is called once per frame
void Update ()
scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition,
targetHorizontalPosition, Time.deltaTime*smoothing);
}
public void OnBeginDrag(PointerEventData eventData)
{
isDraging = true;
}
public void OnEndDrag(PointerEventData eventData)
{
isDraging = false;
float posX = scrollRect.horizontalNormalizedPosition;
int index = 0;
float offset = Mathf.Abs(pageArray[index] - posX);
for (int i = 1; i < pageArray.Length; i++)
{
float offsetTemp = Mathf.Abs(pageArray[i] - posX);
if (offsetTemp < offset)
{
index = i;
offset = offsetTemp;
}
}
targetHorizontalPosition = pageArray[index];
}
// 四个单选要注册上
public void MoveToPage1(bool isOn) {
if (isOn)
{
targetHorizontalPosition = pageArray[0];
}
}
public void MoveToPage2(bool isOn) {
if (isOn) {
targetHorizontalPosition = pageArray[1];
}
}
public void MoveToPage3(bool isOn) {
if (isOn)
{
targetHorizontalPosition = pageArray[2];
}
}
public void MoveToPage4(bool isOn) {
if (isOn)
{
targetHorizontalPosition = pageArray[3];
}
}
}
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
//添加了两个拖拽开始拖拽结束接口,对滑动进行检测
public class LevelButtonScrollRect : MonoBehaviou,IBeginDragHandler,IEndDragHandler
{
private ScrollRect scrollRect;
public float smoothing = 4;
//3页 所以是1/3,0,0.33f,0.66f,1
private float[] pageArray=new float[]{ 0,0.33333f,0.66666f,1 };
//选择组 当移动图片的时候,选择组会对应的打开
public Toggle[] toggleArray;private float targetHorizontalPosition=0;
private bool isDraging = false;
// Use this for initialization
void Start ()
{
scrollRect = GetComponent<ScrollRect>();
}
// Update is called once per frame
void Update ()
{
//结束滑动的时候开始移动
if(isDraging==false)scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition,
targetHorizontalPosition, Time.deltaTime*smoothing);
}
public void OnBeginDrag(PointerEventData eventData)
{
isDraging = true;
}
public void OnEndDrag(PointerEventData eventData)
{
isDraging = false;
float posX = scrollRect.horizontalNormalizedPosition;
int index = 0;
float offset = Mathf.Abs(pageArray[index] - posX);
for (int i = 1; i < pageArray.Length; i++)
{
float offsetTemp = Mathf.Abs(pageArray[i] - posX);
if (offsetTemp < offset)
{
index = i;
offset = offsetTemp;
}
}
targetHorizontalPosition = pageArray[index];
//对应的单选按钮的isOn属性开启
toggleArray[index].isOn = true;
//scrollRect.horizontalNormalizedPosition = pageArray[index];}
// 四个单选要注册上
public void MoveToPage1(bool isOn) {
if (isOn)
{
targetHorizontalPosition = pageArray[0];
}
}
public void MoveToPage2(bool isOn) {
if (isOn) {
targetHorizontalPosition = pageArray[1];
}
}
public void MoveToPage3(bool isOn) {
if (isOn)
{
targetHorizontalPosition = pageArray[2];
}
}
public void MoveToPage4(bool isOn) {
if (isOn)
{
targetHorizontalPosition = pageArray[3];
}
}
}