实现类似ios闹钟设置界面选择时间效果

滑动选择时间效果

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class Test1 : MonoBehaviour,IBeginDragHandler,IDragHandler
{
    /// <summary>
    /// 根据手指滑动距离得出一个比率来计算方法执行次数
    /// </summary>
    public Text text1, text2, text3;
    private int count1,count2,count3;
    private Vector2 oldPos,newPos;

    public void OnBeginDrag(PointerEventData eventData)
    {
        oldPos = eventData.position;
    }

    public void OnDrag(PointerEventData eventData)
    {
        newPos = eventData.position;
        if ((newPos - oldPos).magnitude >= 20)
        {
            //DoLoop1(newPos.y - oldPos.y);
            DoNotLoop1(newPos.y - oldPos.y);
            oldPos = newPos;
        }
    }

    void Start()
    {
        count1 = 0;
        count2 = 1;
        count3 = 2;
    }

    void Update()
    {
        
    }

    public void DoNotLoop()
    {
        //未实现数字循环
        if (text1.text != "")//设置临界点
        {
            if (Input.GetKeyDown(KeyCode.A) && int.Parse(text1.text) >= 0)
            {
                if (int.Parse(text1.text) == 0)
                {
                    text1.text = "";
                    text2.text = (int.Parse(text2.text) - 1).ToString();
                    text3.text = (int.Parse(text3.text) - 1).ToString();
                }
                else
                {
                    text1.text = (int.Parse(text1.text) - 1).ToString();
                    text2.text = (int.Parse(text2.text) - 1).ToString();
                    if (text3.text == "")//变为空串后需要赋值字符串
                    {
                        text3.text = "23";
                    }
                    else
                    {
                        text3.text = (int.Parse(text3.text) - 1).ToString();
                    }
                }
            }
        }
        if (text3.text != "")//设置临界点
        {
            if (Input.GetKeyDown(KeyCode.B) && int.Parse(text3.text) <= 23)
            {
                if (int.Parse(text3.text) == 23)
                {
                    text1.text = (int.Parse(text1.text) + 1).ToString();
                    text2.text = (int.Parse(text2.text) + 1).ToString();
                    text3.text = "";
                }
                else
                {
                    if (text1.text == "")//变为空串后需要赋值字符串
                    {
                        text1.text = "0";
                    }
                    else
                    {
                        text1.text = (int.Parse(text1.text) + 1).ToString();
                    }
                    text2.text = (int.Parse(text2.text) + 1).ToString();
                    text3.text = (int.Parse(text3.text) + 1).ToString();
                }
            }
        }
    }

    public void DoNotLoop1(float y)
    {
        //未实现数字循环
        if (y < 0)
        {
            if (text1.text != "")//设置临界点
            {
                if (int.Parse(text1.text) >= 0)
                {
                    if (int.Parse(text1.text) == 0)
                    {
                        text1.text = "";
                        text2.text = (int.Parse(text2.text) - 1).ToString();
                        text3.text = (int.Parse(text3.text) - 1).ToString();
                    }
                    else
                    {
                        text1.text = (int.Parse(text1.text) - 1).ToString();
                        text2.text = (int.Parse(text2.text) - 1).ToString();
                        if (text3.text == "")//变为空串后需要赋值字符串
                        {
                            text3.text = "23";
                        }
                        else
                        {
                            text3.text = (int.Parse(text3.text) - 1).ToString();
                        }
                    }
                }
            }
        }
        if (y > 0)
        {
            if (text3.text != "")//设置临界点
            {
                if (int.Parse(text3.text) <= 23)
                {
                    if (int.Parse(text3.text) == 23)
                    {
                        text1.text = (int.Parse(text1.text) + 1).ToString();
                        text2.text = (int.Parse(text2.text) + 1).ToString();
                        text3.text = "";
                    }
                    else
                    {
                        if (text1.text == "")//变为空串后需要赋值字符串
                        {
                            text1.text = "0";
                        }
                        else
                        {
                            text1.text = (int.Parse(text1.text) + 1).ToString();
                        }
                        text2.text = (int.Parse(text2.text) + 1).ToString();
                        text3.text = (int.Parse(text3.text) + 1).ToString();
                    }
                }
            }
        }
    }

    public void DoLoop()
    {
        //实现数字循环
        if (Input.GetKeyDown(KeyCode.A))
        {
            if (--count1 != -1 && count1 >= 0)
            {
                ++count1;
                count1 -= 1;
                text1.text = count1.ToString();
            }
            else
            {
                count1 = 23;
                text1.text = count1.ToString();
            }

            if (--count2 != -1 && count2 >= 0)
            {
                ++count2;
                count2 -= 1;
                text2.text = count2.ToString();
            }
            else
            {
                count2 = 23;
                text2.text = count2.ToString();
            }

            if (--count3 != -1 && count3 >= 0)
            {
                ++count3;
                count3 -= 1;
                text3.text = count3.ToString();
            }
            else
            {
                count3 = 23;
                text3.text = count3.ToString();
            }
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
            if (++count1 != 24 && count1 <= 23)
            {
                --count1;
                count1 += 1;
                text1.text = count1.ToString();
            }
            else
            {
                count1 = 0;
                text1.text = count1.ToString();
            }

            if (++count2 != 24 && count2 <= 23)
            {
                --count2;
                count2 += 1;
                text2.text = count2.ToString();
            }
            else
            {
                count2 = 0;
                text2.text = count2.ToString();
            }

            if (++count3 != 24 && count3 <= 23)
            {
                --count3;
                count3 += 1;
                text3.text = count3.ToString();
            }
            else
            {
                count3 = 0;
                text3.text = count3.ToString();
            }
        }
    }

    public void DoLoop1(float y)
    {
        //实现数字循环
        if (y < 0)
        {
            if (--count1 != -1 && count1 >= 0)
            {
                ++count1;
                count1 -= 1;
                text1.text = count1.ToString();
            }
            else
            {
                count1 = 23;
                text1.text = count1.ToString();
            }

            if (--count2 != -1 && count2 >= 0)
            {
                ++count2;
                count2 -= 1;
                text2.text = count2.ToString();
            }
            else
            {
                count2 = 23;
                text2.text = count2.ToString();
            }

            if (--count3 != -1 && count3 >= 0)
            {
                ++count3;
                count3 -= 1;
                text3.text = count3.ToString();
            }
            else
            {
                count3 = 23;
                text3.text = count3.ToString();
            }
        }

        if (y > 0)
        {
            if (++count1 != 24 && count1 <= 23)
            {
                --count1;
                count1 += 1;
                text1.text = count1.ToString();
            }
            else
            {
                count1 = 0;
                text1.text = count1.ToString();
            }

            if (++count2 != 24 && count2 <= 23)
            {
                --count2;
                count2 += 1;
                text2.text = count2.ToString();
            }
            else
            {
                count2 = 0;
                text2.text = count2.ToString();
            }

            if (++count3 != 24 && count3 <= 23)
            {
                --count3;
                count3 += 1;
                text3.text = count3.ToString();
            }
            else
            {
                count3 = 0;
                text3.text = count3.ToString();
            }
        }
    }
}
 
``

 1. 需要三个Text,第一个跟最后一个的Text文本颜色跟中间Text的文本颜色不同,效果就类似列表滑动
 
 ![时间选择器](https://img-blog.csdnimg.cn/20200211123945567.PNG)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值