36 lines
1.4 KiB
C#
36 lines
1.4 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
|
|||
|
|
public class ScrollViewVerticalMoveItem : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
public ScrollRect scrollRect; // <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA> ScrollRect <20>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
public float cellHeight = 100f; // ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD>ӵĸ߶ȣ<DFB6><C8A3>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
public void MoveDown()
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>λ<EFBFBD><CEBB>
|
|||
|
|
float newY = scrollRect.content.anchoredPosition.y + cellHeight;
|
|||
|
|
// <20><>ȡContent<6E>ĸ߶ȺͿ<C8BA><CDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ߶<C4B8>
|
|||
|
|
float contentHeight = scrollRect.content.sizeDelta.y;
|
|||
|
|
float viewportHeight = scrollRect.viewport.rect.height;
|
|||
|
|
// ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Y ֵ
|
|||
|
|
newY = Mathf.Min(newY, contentHeight - viewportHeight);
|
|||
|
|
// Smoothly move to the new position
|
|||
|
|
StartCoroutine(SmoothMove(scrollRect.content.anchoredPosition.y, newY));
|
|||
|
|
}
|
|||
|
|
private IEnumerator SmoothMove(float startY, float targetY)
|
|||
|
|
{
|
|||
|
|
float elapsedTime = 0.0f;
|
|||
|
|
float duration = 0.3f; // <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
while (elapsedTime < duration)
|
|||
|
|
{
|
|||
|
|
float newY = Mathf.Lerp(startY, targetY, (elapsedTime / duration));
|
|||
|
|
scrollRect.content.anchoredPosition = new Vector2(scrollRect.content.anchoredPosition.x, newY);
|
|||
|
|
elapsedTime += Time.deltaTime;
|
|||
|
|
yield return null;
|
|||
|
|
}
|
|||
|
|
// ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>ȷ
|
|||
|
|
scrollRect.content.anchoredPosition = new Vector2(scrollRect.content.anchoredPosition.x, targetY);
|
|||
|
|
}
|
|||
|
|
}
|