FairyGUI.ScrollPane.__touchEnd C# (CSharp) Method

__touchEnd() private method

private __touchEnd ( EventContext context ) : void
context EventContext
return void
        private void __touchEnd(EventContext context)
        {
            InputEvent evt = context.inputEvent;
            if (_touchId != evt.touchId)
                return;

            Stage.inst.onTouchMove.Remove(_touchMoveDelegate);
            Stage.inst.onTouchEnd.Remove(_touchEndDelegate);

            if (draggingPane == this)
                draggingPane = null;

            _gestureFlag = 0;

            if (!_isMouseMoved || _owner.displayObject == null || _owner.displayObject.isDisposed
                 || !_touchEffect || _inertiaDisabled)
            {
                _isMouseMoved = false;
                return;
            }

            _isMouseMoved = false;
            float time = Time.time - _time2;
            if (time == 0)
                time = 0.001f;
            float yVelocity = (_container.y - _y2) / time * 2 * UIConfig.defaultTouchScrollSpeedRatio;
            float xVelocity = (_container.x - _x2) / time * 2 * UIConfig.defaultTouchScrollSpeedRatio;
            float duration = 0.3f;

            _throwTween.start.x = _container.x;
            _throwTween.start.y = _container.y;

            Vector2 change1, change2;
            float endX = 0, endY = 0;
            int fireRelease = 0;

            if (_scrollType == ScrollType.Both || _scrollType == ScrollType.Horizontal)
            {
                if (_container.x > UIConfig.touchDragSensitivity)
                    fireRelease = 1;
                else if (_container.x < -_xOverlap - UIConfig.touchDragSensitivity)
                    fireRelease = 2;

                change1.x = ThrowTween.CalculateChange(xVelocity, duration);
                change2.x = 0;
                endX = _container.x + change1.x;

                if (_pageMode && endX < 0 && endX > -_xOverlap)
                {
                    int page = Mathf.FloorToInt(-endX / _pageSize.x);
                    float testPageSize = Mathf.Min(_pageSize.x, _contentWidth - (page + 1) * _pageSize.x);
                    float delta = -endX - page * _pageSize.x;
                    //页面吸附策略
                    if (Mathf.Abs(change1.x) > _pageSize.x)//如果滚动距离超过1页,则需要超过页面的一半,才能到更下一页
                    {
                        if (delta > testPageSize * 0.5f)
                            page++;
                    }
                    else //否则只需要页面的1/3,当然,需要考虑到左移和右移的情况
                    {
                        if (delta > testPageSize * (change1.x < 0 ? 0.3f : 0.7f))
                            page++;
                    }

                    //重新计算终点
                    endX = -page * _pageSize.x;
                    if (endX < -_xOverlap) //最后一页未必有_pageSizeH那么大
                        endX = -_xOverlap;

                    change1.x = endX - _container.x;
                }
            }
            else
                change1.x = change2.x = 0;

            if (_scrollType == ScrollType.Both || _scrollType == ScrollType.Vertical)
            {
                if (_container.y > UIConfig.touchDragSensitivity)
                    fireRelease = 1;
                else if (_container.y < -_yOverlap - UIConfig.touchDragSensitivity)
                    fireRelease = 2;

                change1.y = ThrowTween.CalculateChange(yVelocity, duration);
                change2.y = 0;
                endY = _container.y + change1.y;

                if (_pageMode && endY < 0 && endY > -_yOverlap)
                {
                    int page = Mathf.FloorToInt(-endY / _pageSize.y);
                    float testPageSize = Mathf.Min(_pageSize.y, _contentHeight - (page + 1) * _pageSize.y);
                    float delta = -endY - page * _pageSize.y;
                    if (Mathf.Abs(change1.y) > _pageSize.y)
                    {
                        if (delta > testPageSize * 0.5f)
                            page++;
                    }
                    else
                    {
                        if (delta > testPageSize * (change1.y < 0 ? 0.3f : 0.7f))
                            page++;
                    }

                    endY = -page * _pageSize.y;
                    if (endY < -_yOverlap)
                        endY = -_yOverlap;

                    change1.y = endY - _container.y;
                }
            }
            else
                change1.y = change2.y = 0;

            if (_snapToItem && !_pageMode)
            {
                endX = -endX;
                endY = -endY;
                _owner.GetSnappingPosition(ref endX, ref endY);
                endX = -endX;
                endY = -endY;
                change1.x = endX - _container.x;
                change1.y = endY - _container.y;
            }

            if (_bouncebackEffect)
            {
                if (endX > 0)
                    change2.x = 0 - _container.x - change1.x;
                else if (endX < -_xOverlap)
                    change2.x = -_xOverlap - _container.x - change1.x;

                if (endY > 0)
                    change2.y = 0 - _container.y - change1.y;
                else if (endY < -_yOverlap)
                    change2.y = -_yOverlap - _container.y - change1.y;
            }
            else
            {
                if (endX > 0)
                    change1.x = 0 - _container.x;
                else if (endX < -_xOverlap)
                    change1.x = -_xOverlap - _container.x;

                if (endY > 0)
                    change1.y = 0 - _container.y;
                else if (endY < -_yOverlap)
                    change1.y = -_yOverlap - _container.y;
            }

            _throwTween.value = 0;
            _throwTween.change1 = change1;
            _throwTween.change2 = change2;

            if (_tweener != null)
                KillTween();

            _tweening = 2;
            _tweener = DOTween.To(() => _throwTween.value, v => _throwTween.value = v, 1, duration)
                .SetEase(Ease.OutCubic)
                .SetUpdate(true)
                .OnUpdate(__tweenUpdate2)
                .OnComplete(__tweenComplete2);

            if (fireRelease == 1)
                onPullDownRelease.Call();
            else if (fireRelease == 2)
                onPullUpRelease.Call();
        }