向右滑动关闭界面

这篇博客分享了如何在APP中实现通过手指向右滑动来关闭界面的功能。作者遇到了阻挡下方按钮点击的问题,于是调整了实现方式,通过判断最上层界面来避免干扰。代码中使用了CanvasGroup的blocksRaycasts属性来控制界面的射线检测,并提供了详细的Update方法实现滑动关闭的逻辑。博客包含关键代码片段,适合对移动应用界面交互设计感兴趣的开发者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

向右滑动关闭界面

在一些app或者特殊功能的时候会用到手指向右滑动关闭界面的情况,找了半天没合适的,自己动手,丰衣足食

注释:本来是代码是在界面上拖拽用来执行,后来发现会阻挡下面按钮的触发事件,后改成判断最上层界面

上代码 IsBlocksPaycasts = CanvasGroup的blocksRaycasts 用来在关闭的时候关闭界面的射线检测

下面展示一些 内联代码片

	private Vector3 offect;
    private BasePanel currentPanel;
    private bool isDrag = false;
    
    public void Update()
    {
    	//按下的时候
        if (Input.GetMouseButtonDown(0))
        {
            offect = Input.mousePosition;
            //判断当前的界面,可自行根据实际情况改变
            currentPanel = UIManager.Instance.GetCurrentPanel();
        }
        if (Input.GetMouseButton(0))
        {
            if (currentPanel == null)
            {
                return;
            }
            if (Input.mousePosition.x - offect.x > 100)
            {
                isDrag = true;
                currentPanel.IsBlocksPaycasts = false;
            }
            if (isDrag)
            {
                if (Input.mousePosition.x - offect.x < 0)
                {
                    currentPanel.transform.localPosition = Vector3.zero;
                    return;
                }
                currentPanel.transform.localPosition = Vector3.right * (Input.mousePosition.x - offect.x - 100);
                //Debug.Log(Input.mousePosition.x);
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            if (currentPanel == null)
            {
                return;
            }
            if (!isDrag)
            {
                return;
            }
            if (currentPanel.transform.localPosition.x < Screen.width / 2)
            {
                currentPanel.GetComponent<RectTransform>().DOLocalMove(Vector3.zero, currentPanel.TweenTime);
            }
            else
            {
                currentPanel.CloseTween();
            }
            currentPanel.IsBlocksPaycasts = true;
            isDrag = false;
            offect = Vector3.zero;
            //Debug.Log("弹起");
        }
    }

大功告成~!加鸡腿!!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值