Unity UGUI 分页滑动

2016-10-04 13:45:21

 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEngine.EventSystems;
 4 using System;
 5 using UnityEngine.UI;
 6 public class LevelButtonScrollRect : MonoBehaviour, IBeginDragHandler, IEndDragHandler
 7 {
 8     private ScrollRect scrollRect;
 9     private float[] pageArray = new float[] { 0, 0.333f, 0.666f, 1 };
10     public Toggle[] ToggleArray;
11     private float speed = 5f;
12     private float targetHorizontalPosition = 0f;
13     private bool isDraging = false;
14     void Start()
15     {
16         scrollRect = transform.GetComponent<ScrollRect>();
17     }
18 
19     void Update()
20     {
21         if (!isDraging)
22         {
23             scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition,
24             targetHorizontalPosition, Time.deltaTime * speed);
25         }
26         
27     }
28     public void OnBeginDrag(PointerEventData eventData)
29     {
30         isDraging = true;
31     }
32     public void OnEndDrag(PointerEventData eventData)
33     {
34         isDraging = false;
35         // 得到 水平滑动的 值  (0-1)
36         float posX = scrollRect.horizontalNormalizedPosition;
37         int index = 0;
38         float offset = Mathf.Abs(posX - pageArray[index]);
39         // 与 前后比较 距离最短
40         for (int i = 1; i < pageArray.Length; i++)
41         {
42             // 距离 最短
43             float offsetTemp = Mathf.Abs(posX - pageArray[i]);
44             if (offset > offsetTemp)
45             {
46                 index = i;
47                 offset = offsetTemp;
48             }
49         }
50         targetHorizontalPosition = pageArray[index];
51         ToggleArray[index].isOn = true;
52     }
53 }

 

转载于:https://www.cnblogs.com/lubei/p/5930410.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值