C# 常用缓动计算

public static class Ease
{
    public static float LinearEase(float start, float changeValue, float currentTime, float fullTime)
    {
        return start + changeValue * currentTime / fullTime;
    }

    public static float QuadraticEaseIn(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime;
        return start + changeValue * currentTime * currentTime;
    }

    public static float QuadraticEaseOut(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime;
        return start - changeValue * currentTime * (currentTime - 2);
    }

    public static float QuadraticEaseInOut(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime / 2;
        if (currentTime < 1) return start + changeValue / 2 * currentTime * currentTime;
        currentTime--;
        return start - changeValue / 2 * (currentTime * (currentTime - 2) - 1);
    }

    public static float CubicEaseIn(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime;
        return start + changeValue * currentTime * currentTime * currentTime;
    }

    public static float CubicEaseOut(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime;
        currentTime--;
        return start + changeValue * (currentTime * currentTime * currentTime + 1);
    }

    public static float CubicEaseInOut(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime / 2;
        if (currentTime < 1) return start + changeValue / 2 * currentTime * currentTime * currentTime;
        currentTime -= 2;
        return start + changeValue / 2 * (currentTime * currentTime * currentTime + 2);
    }

    public static float QuarticEaseIn(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime;
        return start + changeValue * currentTime * currentTime * currentTime * currentTime;
    }

    public static float QuarticEaseOut(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime;
        currentTime--;
        return start - changeValue * (currentTime * currentTime * currentTime * currentTime - 1);
    }

    public static float QuarticEaseInOut(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime / 2;
        if (currentTime < 1) return start + changeValue / 2 * currentTime * currentTime * currentTime * currentTime;
        currentTime -= 2;
        return start - changeValue / 2 * (currentTime * currentTime * currentTime * currentTime - 2);
    }

    public static float QuinticEaseIn(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime;
        return start + changeValue * currentTime * currentTime * currentTime * currentTime * currentTime;
    }

    public static float QuinticEaseOut(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime;
        currentTime--;
        return start + changeValue * (currentTime * currentTime * currentTime * currentTime * currentTime + 1);
    }

    public static float QuinticEaseInOut(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime / 2;
        if (currentTime < 1) return start + changeValue / 2 * currentTime * currentTime * currentTime * currentTime * currentTime;
        currentTime -= 2;
        return start + changeValue / 2 * (currentTime * currentTime * currentTime * currentTime * currentTime + 2);
    }

    public static float ExponentialEaseIn(float start, float changeValue, float currentTime, float fullTime)
    {
        return start + changeValue * (float)System.Math.Pow(2, 10 * (currentTime / fullTime - 1));
    }

    public static float ExponentialEaseOut(float start, float changeValue, float currentTime, float fullTime)
    {
        return start + changeValue * (float)(-System.Math.Pow(2, -10 * currentTime / fullTime) + 1);
    }

    public static float ExponentialEaseInOut(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime / 2;
        if (currentTime < 1) return start + changeValue / 2 * (float)System.Math.Pow(2, 10 * (currentTime - 1));
        currentTime--;
        return start + changeValue / 2 * (float)(-System.Math.Pow(2, -10 * currentTime) + 2);
    }

    public static float CircularEaseIn(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime;
        return start - changeValue * (float)(System.Math.Sqrt(1 - currentTime * currentTime) - 1);
    }

    public static float CircularEaseOut(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime;
        currentTime--;
        return start + changeValue * (float)System.Math.Sqrt(1 - currentTime * currentTime);
    }

    public static float CircularEaseInOut(float start, float changeValue, float currentTime, float fullTime)
    {
        currentTime /= fullTime / 2;
        if (currentTime < 1) return start - changeValue / 2 * (float)(System.Math.Sqrt(1 - currentTime * currentTime) - 1);
        currentTime -= 2;
        return start + changeValue / 2 * (float)(System.Math.Sqrt(1 - currentTime * currentTime) + 1);
    }

    public static float SinusoidalEaseIn(float start, float changeValue, float currentTime, float fullTime)
    {
        return start + changeValue - changeValue * (float)System.Math.Cos(currentTime / fullTime * (System.Math.PI / 2));
    }

    public static float SinusoidalEaseOut(float start, float changeValue, float currentTime, float fullTime)
    {
        return start + changeValue * (float)System.Math.Sin(currentTime / fullTime * (System.Math.PI / 2));
    }

    public static float SinusoidalEaseInOut(float start, float changeValue, float currentTime, float fullTime)
    {
        return start - changeValue / 2 * (float)(System.Math.Cos(System.Math.PI * currentTime / fullTime) - 1);
    }

    public static float DoEase(EaseType type, float start, float changeValue, float currentTime, float fullTime)
    {
        switch (type)
        {
            case EaseType.Linear:
                return LinearEase(start, changeValue, currentTime, fullTime);
            case EaseType.QuadraticIn:
                return QuadraticEaseIn(start, changeValue, currentTime, fullTime);
            case EaseType.QuadraticOut:
                return QuadraticEaseOut(start, changeValue, currentTime, fullTime);
            case EaseType.QuadraticInOut:
                return QuarticEaseInOut(start, changeValue, currentTime, fullTime);
            case EaseType.CubicIn:
                return CubicEaseIn(start, changeValue, currentTime, fullTime);
            case EaseType.CubicOut:
                return CubicEaseOut(start, changeValue, currentTime, fullTime);
            case EaseType.CubicInOut:
                return CubicEaseInOut(start, changeValue, currentTime, fullTime);
            case EaseType.QuarticIn:
                return QuarticEaseIn(start, changeValue, currentTime, fullTime);
            case EaseType.QuarticOut:
                return QuarticEaseOut(start, changeValue, currentTime, fullTime);
            case EaseType.QuarticInOut:
                return QuarticEaseInOut(start, changeValue, currentTime, fullTime);
            case EaseType.QuinticIn:
                return QuinticEaseIn(start, changeValue, currentTime, fullTime);
            case EaseType.QuinticOut:
                return QuinticEaseOut(start, changeValue, currentTime, fullTime);
            case EaseType.QuinticInOut:
                return QuinticEaseInOut(start, changeValue, currentTime, fullTime);
            case EaseType.ExponentialIn:
                return ExponentialEaseIn(start, changeValue, currentTime, fullTime);
            case EaseType.ExponentialOut:
                return ExponentialEaseOut(start, changeValue, currentTime, fullTime);
            case EaseType.ExponentialInOut:
                return ExponentialEaseInOut(start, changeValue, currentTime, fullTime);
            case EaseType.CircularIn:
                return CircularEaseIn(start, changeValue, currentTime, fullTime);
            case EaseType.CircularOut:
                return CircularEaseOut(start, changeValue, currentTime, fullTime);
            case EaseType.CircularInOut:
                return CircularEaseInOut(start, changeValue, currentTime, fullTime);
            case EaseType.SinusoidalIn:
                return SinusoidalEaseIn(start, changeValue, currentTime, fullTime);
            case EaseType.SinusoidalOut:
                return SinusoidalEaseOut(start, changeValue, currentTime, fullTime);
            case EaseType.SinusoidalInOut:
                return SinusoidalEaseInOut(start, changeValue, currentTime, fullTime);
            default:
                return LinearEase(start, changeValue, currentTime, fullTime);
        }
    }
}


public enum EaseType
{
    Linear,
    QuadraticIn,
    QuadraticOut,
    QuadraticInOut,
    CubicIn,
    CubicOut,
    CubicInOut,
    QuarticIn,
    QuarticOut,
    QuarticInOut,
    QuinticIn,
    QuinticOut,
    QuinticInOut,
    ExponentialIn,
    ExponentialOut,
    ExponentialInOut,
    CircularIn,
    CircularOut,
    CircularInOut,
    SinusoidalIn,
    SinusoidalOut,
    SinusoidalInOut
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值