Scroll 单个目标居中显示
李鸿
2023-12-01
enum TimeType
{
Branch,//分
Hour,//时
}
public delegate void SuccessForDay(List<int> _list);
public class ControlUnionBossTime
{
public static ControlUnionBossTime _instance;
public int Key;//星期几的key;
private const int LABEL_COUNT = 44;
private int[] mHourArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 };
private int[] mBranchArray = { 0, 15, 30, 45 };
private CLabel mHourLabel;
private CLabel mBranch;
private CButton mCloseBtn;
private CButton mSureBtn;
private UIScrollView mHourScroll;
private UIScrollView mBranchScroll;
private CPanel mpanel;
private GameObject _obj;
public event SuccessForDay Event_SuccessDay;
// private CButton mMask;
private UIPanel mBranchPanel;
private UIPanel mHourPanel;
private float yLvVelocity = 0, yQualityVelocity = 0;
private const float SMOOTHTIME = 0.01f;//平花时间
private const int BRANCH_MIN = -88;
private const int BRANCH_MAX = 44;
private const int HOUR_MIN = -44;
private const int HOUR_MAX = 968;
private int mCurChooseBranch;
private int mCurChooseHour;
private float mScrollBranchChooseY;//分钟的目标值
private float mScrollHourChooseY; //小时的目标值
private bool isDragingBrach;
private bool isDragingHour;
public List<int> currentTime;
StringBuilder str;
public ControlUnionBossTime(CPanel cpanel)
{
if (_instance == null) _instance = this;
mpanel = cpanel;
_obj = mpanel.FindChild("Detail/right/Time");
mCloseBtn = mpanel.GetElementBase("(Button)Uniontime_CloseButton") as CButton;
mCloseBtn.Depth = 500;
mSureBtn = mpanel.GetElementBase("(Button)Sure_btn") as CButton;
//mMask = mpanel.GetElementBase("(Button)Bg") as CButton;
//mMask.SetClickDelegate(Btn_click);
mHourScroll = mCloseBtn.gameobject.transform.parent.Find("HourTimeScorview/Hour/hourView").GetComponent<UIScrollView>();
mHourScroll.movement = UIScrollView.Movement.Vertical;
mHourPanel = mHourScroll.transform.GetComponent<UIPanel>();
mBranchScroll = mCloseBtn.gameobject.transform.parent.Find("BrankTimeScorvIew/Branch/branchView").GetComponent<UIScrollView>();
mBranchPanel = mBranchScroll.transform.GetComponent<UIPanel>();
mHourLabel = mpanel.GetElementBase("(Label)HourLabel") as CLabel;
mBranch = mpanel.GetElementBase("(Label)branchLabel") as CLabel;
mHourLabel.Text = CreateBranchLabel(TimeType.Hour);
mBranch.Text = CreateBranchLabel(TimeType.Branch);
mCloseBtn.SetClickDelegate(Btn_click);
mSureBtn.SetClickDelegate(Btn_click);
mHourScroll.onDragFinished = OnScrollViewHourFinsh;
mBranchScroll.onDragFinished = OnScrollViewBrachFinsh;
GameObject brachDrag = mBranch.gameobject.GetComponent<UIDragScrollView>().gameObject;
GameObject obj = mHourLabel.gameobject.transform.GetComponent<UIDragScrollView>().gameObject;
UIEventListener.Get(obj).onDrag = OnHourScroll;
UIEventListener.Get(brachDrag).onDrag = OnBrachScroll;
IninPanel();
}
public void IninPanel()
{
if (currentTime == null)
currentTime = new List<int>();
else
currentTime.Clear();
currentTime.Add(1);
currentTime.Add(30);
isDragingBrach = true;
isDragingHour = true;
SetScrollViewPos(mBranchPanel, 0);//默认位置;
SetScrollViewPos(mHourPanel, 0);
CCommon.SetStaticLabel(mpanel, "(Label)Boss_Time_tip_title", 97110029);
CCommon.SetStaticLabel(mpanel, "(Labelboss_local_time", 97110031);
CCommon.SetStaticLabel(mpanel, "(Label)Sure_tip", 97110021);
}
public void Update()
{
if (!_obj.activeSelf)
return;
if (!isDragingBrach)
{
if (-mBranchScroll.panel.clipOffset.y != mScrollBranchChooseY)
{
float ytemp = Mathf.SmoothDamp(-mBranchScroll.panel.clipOffset.y, mScrollBranchChooseY,
ref yLvVelocity, SMOOTHTIME);
if (Mathf.Abs(-mBranchScroll.panel.clipOffset.y - mScrollBranchChooseY) < 0.1f)
{
ytemp = mScrollBranchChooseY;
isDragingBrach = true;
}
SetScrollViewPos(mBranchScroll.panel, ytemp);
}
}
if (!isDragingHour)
{
if (-mHourScroll.panel.clipOffset.y != mScrollHourChooseY)
{
float ytemp = Mathf.SmoothDamp(-mHourScroll.panel.clipOffset.y, mScrollHourChooseY,
ref yQualityVelocity, SMOOTHTIME);
if (Mathf.Abs(-mHourScroll.panel.clipOffset.y - mScrollHourChooseY) < 0.1f)
{
ytemp = mScrollHourChooseY;
isDragingHour = true;
}
SetScrollViewPos(mHourScroll.panel, ytemp);
}
}
}
string CreateBranchLabel(TimeType type)
{
if (str == null) str = new StringBuilder();
str.Remove(0, str.Length);
if (type == TimeType.Branch)
{
for (int i = 0; i < mBranchArray.Length; i++)
{
str.Append(mBranchArray[i] + "分");
if (i != (mBranchArray.Length - 1))
{
str.Append("\n");
}
}
return str.ToString();
}
else if (type == TimeType.Hour)
{
for (int i = 0; i < mBranchArray.Length; i++)
{
str.Append(mHourArray[i] + "点");
if (i != (mHourArray.Length - 1))
{
str.Append("\n");
}
}
return str.ToString();
}
return "";
}
private void SetScrollViewPos(UIPanel panel,float y)
{
//设置当前的目标值
//判断是否为40的倍数
panel.transform.localPosition = new Vector3(panel.transform.localPosition.x, y, panel.transform.localPosition.z);
panel.clipOffset = new Vector3(-panel.transform.localPosition.x, -y, -panel.transform.localPosition.z);
SetCurrentBranch();
}
public float CheckScrollViewAimY(UIScrollView _scrollView, float scrollAimY)
{
float localnum = LABEL_COUNT / 2; //取半;
float offset;
float sym = scrollAimY >= 0 ? 1 : -1;
offset = scrollAimY % LABEL_COUNT; //偏移量
float determineY = scrollAimY - offset; //前半部分
if (Mathf.Abs(offset) >= localnum)
{
offset = sym * LABEL_COUNT;
}
else
offset = 0;
float resultY = determineY + offset;
if (_scrollView == mBranchScroll)
resultY = Mathf.Clamp(resultY, BRANCH_MIN, BRANCH_MAX);
else if (_scrollView == mHourScroll)
resultY = Mathf.Clamp(resultY, HOUR_MIN, HOUR_MAX);
return resultY;
}
public void OnBrachScroll(GameObject go, Vector2 v2)
{
isDragingBrach = true;
}
public void OnHourScroll(GameObject go, Vector2 v2)
{
isDragingHour = true;
}
public void OnScrollViewBrachFinsh()
{
mScrollBranchChooseY = CheckScrollViewAimY(mBranchScroll ,- mBranchScroll.panel.clipOffset.y);
isDragingBrach = false;
// Update();
}
public void OnScrollViewHourFinsh()
{
mScrollHourChooseY = CheckScrollViewAimY(mHourScroll, -mHourScroll.panel.clipOffset.y);
isDragingHour = false;
}
public void Shwo()
{
_obj.SetActive(true);
}
public void Hide()
{
_obj.SetActive(false);
}
void Btn_click(CButton btn)
{
if(btn.Equals(mCloseBtn))
{
Hide();
}
if (btn.Equals(mSureBtn))
{
Event_SuccessDay.Invoke(currentTime);
Hide();
}
}
private void SetCurrentBranch()
{
int idx = ((int)mScrollBranchChooseY - BRANCH_MIN) / LABEL_COUNT;
mCurChooseBranch = mBranchArray[idx];
str.Remove(0, str.Length);
for (int i = 0; i < mBranchArray.Length; i++)
{
if (i == idx)
{
str.Append("[44ca1a]" + mCurChooseBranch + "分[-]\n");
currentTime[1] = mBranchArray[i];
}
else
str.Append(mBranchArray[i] + "分\n");
}
mBranch.Text = str.ToString();
idx = ((int)mScrollHourChooseY - HOUR_MIN) / LABEL_COUNT;
mCurChooseHour = mHourArray[idx];
str.Remove(0, str.Length);
for (int i = 0; i <mHourArray.Length; i++)
{
if (i == idx)
{
str.Append("[44ca1a]" + mCurChooseHour + "点[-]\n");
currentTime[0] = mHourArray[i];
}
else
str.Append(mHourArray[i] + "点\n");
}
mHourLabel.Text = str.ToString();
}
}