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

被折叠的 条评论
为什么被折叠?



