Unity重写Scroll View 实现单个视频滑动

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class MyVideoScrollRect : ScrollRect
{
    /// <summary>
    /// 滑动开关
    /// </summary>
    private bool slideSwitch = false;

    public static Action nextPage;

    /// <summary>
    /// 从第几个开始页码不再改变   中间位置就是最大页数除以2
    /// </summary>
    public int fixationPos=0;

    protected override void Awake()
    {
        nextPage = AutomaticallySlidesToTheNextPage;
        //设置最大高
        singlePageHeight = CanvasTool.GetCanvasHeight();
        //设置最大view 数量
        pageMaxNumber = VideoManager.MaxVideoNumber;
    }


    /// <summary>
    /// 删除已过时的 VideoView 回调 
    /// </summary>
    public static Action DeleteTimeedViewCallBack;

    /// <summary>
    /// 移动结束回调 
    /// </summary>
    public static Action<int> MoveToEndCallBack;


    /// <summary>
    /// 是否移动结束  移动结束后才能进行下次滑动
    /// </summary>
    public bool weatherMoverEnd=true;

    /// <summary>
    /// 页面滑动的速度
    /// </summary>
    private float speedPage=20f;

    /// <summary>
    /// 拖拽方向
    /// </summary>
    private enum DragDirction
    {
        DOWN,
        UP
    }

    /// <summary>
    /// 本次拖拽的方向
    /// </summary>
    private DragDirction currentDirction {

        get
        {
            if (endY<0)
            {
                return DragDirction.UP;
            }
            return endY - startY >= 0 ? DragDirction.DOWN : DragDirction.UP;
        }
    }

    /// <summary>
    /// 本次拖拽的距离
    /// </summary>
    private float thisDragDistance
    {
        get {

            if (endY<0)
            {
                //返回一个绝对比  dragLenght 小的值  
                return 0;
            }

            return endY - startY;
        }
    }

    /// <summary>
    /// 拖拽可以切换页面 的目标距离  小于这个距离返回原本位置  大于这个距离移动到目标位置
    /// </summary>
    private float dragTargetDistance {

        get
        {
            return singlePageHeight/4f;
        }
    }

    /// <summary>
    /// 单页高度
    /// </summary>
    private float singlePageHeight;

    /// <summary>
    /// 最大页码
    /// </summary>
    private int pageMaxNumber;

    private int _pageNumber = 0;

    /// <summary>
    /// 当前页码
    /// </summary>
    public int PageNumber {

        get {
            return _pageNumber;
        }
        set {
            
            //页码从0 开始不能小于0  页码到达最中间的时候 会添加最后的页面  和删除第一个页面不需要增加页码
            if (value>=0)
            {
                _pageNumber = value;
            }
        }
    }

    ///<summary>
    ///开始Y值
    /// </summary>
    private float startY;

    /// <summary>
    /// 结束的Y值
    /// </summary>
    private float endY;


   

    public override void OnBeginDrag(PointerEventData eventData) {

        if (!slideSwitch)
        {
            return;
        }
        if (!weatherMoverEnd)
        {
            return;
        }

        base.OnBeginDrag(eventData);
        startY = content.position.y;
        
    }

    public override void OnEndDrag(PointerEventData eventData)
    {
        if (!slideSwitch)
        {
            return;
        }
        if (!weatherMoverEnd)
        {
            return;
        }
        endY = content.position.y;
        weatherMoverEnd = false;
        OnEndDragDispose();
        
    }

    public override void OnDrag(PointerEventData eventData) {
        if (!slideSwitch)
        {
            return;
        }
        if (!weatherMoverEnd)
        {
            return;
        }
        base.OnDrag(eventData);
    }

    Coroutine moveCoroutine;


    /// <summary>
    /// 拖拽结束处理
    /// </summary>
    public void OnEndDragDispose()
    {
        //滑动距离是否足够
        if (Mathf.Abs(thisDragDistance) >= dragTargetDistance)
        {
            //足够滑动到目标页
            if (currentDirction==DragDirction.DOWN)
            {
                SlideToTheNextDownPage();
            }
            else
            {
                SlideToTheNextUPPage();
            }
        }
        else
        {
            //不足返回当前页
            moveCoroutine = StartCoroutine(MoveTargetPage(PageNumber * singlePageHeight));
        }
    }

    /// <summary>
    /// 滑动到上一页
    /// </summary>
    public void SlideToTheNextUPPage()
    {
        //页码减少
        PageNumber--;
        moveCoroutine =StartCoroutine(MoveTargetPage(PageNumber* singlePageHeight));
    }

    /// <summary>
    /// 滑动到下一页
    /// </summary>
    public void SlideToTheNextDownPage()
    {
        //页码增加
        PageNumber++;
        moveCoroutine = StartCoroutine(MoveTargetPage(PageNumber * singlePageHeight));
    }
    
    IEnumerator MoveTargetPage(float targetY)
    {
        while (true)
        {
            content.anchoredPosition = Vector2.Lerp(content.anchoredPosition, new Vector2(content.anchoredPosition.x, targetY),speedPage*Time.deltaTime);
            if (Mathf.Abs(content.anchoredPosition.y- targetY)<10f)
            {
                Debug.Log("目标位置  :"+ targetY);
                content.anchoredPosition = new Vector2(content.anchoredPosition.x, targetY);
            
                if (moveCoroutine != null)
                {
                    if (PageNumber>=fixationPos)
                    {
                        Debug.Log("开始删除 和添加 视频 !");
                        DeleteVideoView();
                    }
                    else
                    {
                        MoveToEndCallBack?.Invoke(PageNumber);
                    }
                    weatherMoverEnd = true;
                    StopCoroutine(moveCoroutine);
                    moveCoroutine = null;
                }
            }
            yield return null;
        }
    }


    /// <summary>
    /// 删除VideoView
    /// </summary>
    public void DeleteVideoView()
    {
        DeleteTimeedViewCallBack?.Invoke();
        PageNumber--;
        MoveToEndCallBack?.Invoke(PageNumber);
        content.anchoredPosition = new Vector2(content.anchoredPosition.x, PageNumber * singlePageHeight);
    }



    /// <summary>
    /// 自动滑动到下一页
    /// </summary>
    public void AutomaticallySlidesToTheNextPage()
    {
        //页码增加
        PageNumber++;
        Debug.Log("移动下一页 "+ PageNumber);
        moveCoroutine = StartCoroutine(MoveTargetPage(PageNumber * singlePageHeight));
    }


}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值