ScrollView 翻页吸附

自留

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScrollView_Page : MonoBehaviour
{

    private ScrollRect ScrollRect;
    public RectTransform content;

    private float originPos;
    private float Content_spacing;
    private float[] allCenterPoints;

    // Use this for initialization
    void Start()
    {
        ScrollRect = GetComponent<ScrollRect>();

        if (ScrollRect.vertical)
        {
            Content_spacing = content.GetComponent<VerticalLayoutGroup>().spacing;
            originPos = content.anchoredPosition.y;
        }
        else if(ScrollRect.horizontal)
        {
            Content_spacing = content.GetComponent<HorizontalLayoutGroup>().spacing;
            originPos = content.anchoredPosition.x;
        }

        //获取每个content的边界 -|————|-
        allCenterPoints = new float[content.childCount + 1];
        for (int i = 0; i < allCenterPoints.Length; i++)
        {
            if (i == 0)
            {
                Debug.Log("check originpos " + originPos);
                allCenterPoints[i] = originPos;
                continue;
            }
            else if(i == 1)
            {
                allCenterPoints[i] = originPos + Vector2ToFloat(content.GetChild(i - 1).GetComponent<RectTransform>().sizeDelta) / 2f;
                continue;
            }

            //上一个边界的一半 + 间隔 + 现在的一半
            allCenterPoints[i] = allCenterPoints[i - 1] + Vector2ToFloat(content.GetChild(i - 1).GetComponent<RectTransform>().sizeDelta) / 2f 
                + Vector2ToFloat(content.GetChild(i - 2).GetComponent<RectTransform>().sizeDelta) / 2f + Content_spacing;
        }

        for (int i = 0; i < allCenterPoints.Length; i++)
        {
            Debug.Log("check Limit " + i + " : " + allCenterPoints[i]);
        }
    }

    private void BackToCurCenter()
    {
        float _curPos = Vector2ToFloat(content.anchoredPosition);
        for (int i = 0; i < allCenterPoints.Length; i++)
        {
            if (_curPos > allCenterPoints[allCenterPoints.Length - 1])
            {
                float _center = (allCenterPoints[allCenterPoints.Length - 1] + allCenterPoints[allCenterPoints.Length - 2]) / 2f - originPos;
                content.anchoredPosition = new Vector2(ScrollRect.horizontal ? _center : 0, ScrollRect.vertical ? _center : 0);
                break;
            }
            if (_curPos < allCenterPoints[1])
            {
                content.anchoredPosition = new Vector2(ScrollRect.horizontal ? originPos : 0, ScrollRect.vertical ? originPos : 0);
                break;
            }

            if (_curPos >= allCenterPoints[i] && _curPos < allCenterPoints[i + 1])
            {
                float _center = (allCenterPoints[i] + allCenterPoints[i + 1]) / 2f - originPos;
                content.anchoredPosition = new Vector2(ScrollRect.horizontal ? _center : 0, ScrollRect.vertical ? _center : 0);*斜体样式*
                break;
            }
        }
    }

    private float Vector2ToFloat(Vector2 _pos)
    {
        return ScrollRect.vertical ? _pos.y : _pos.x;
    }


    public void StartDrag()
    {

    }
    public void EndDrag()
    {

        BackToCurCenter();
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值