objective-c – Ease-In Ease-Out功能

我想要一个等效于UIViewAnimationCurveEaseInOut的缓动函数,但是没有看到Apple将它暴露为函数.

我试过the easeInOutQuad function posted here没有成功.我的Objective-C版本的功能是:

-(float) getEaseInOutValueWithElapsedMillis: (float) t startValue: (float) b endValue: (float) c andTotalMillis: (float) d {
   float   value =0.0f;

   if ((t/=d/2.0f) <1.0f)
      value =c/2.0f*t*t + b;
   else
      value =-c/2.0f * ((--t)*(t-2.0f) - 1.0f) + b;

   debug(@"t=%f b=%f c=%f d=%f value=%f", t, b, c, d, value);
   return value;
}

记录的结果是:

当起始值小于结束值时:

t=0.066467 b=110.000000 c=225.000000 d=500.000000 value=110.497017
t=0.133133 b=110.000000 c=225.000000 d=500.000000 value=111.993996
t=0.199799 b=110.000000 c=225.000000 d=500.000000 value=114.490944
...
t=0.999786 b=110.000000 c=225.000000 d=500.000000 value=222.451935
t=0.066452 b=110.000000 c=225.000000 d=500.000000 value=236.954926
t=0.133118 b=110.000000 c=225.000000 d=500.000000 value=250.457932
... (note that value shot right past c)
t=0.866440 b=110.000000 c=225.000000 d=500.000000 value=332.993195
t=0.933105 b=110.000000 c=225.000000 d=500.000000 value=334.496582
t=0.999771 b=110.000000 c=225.000000 d=500.000000 value=335.000000

并且100%的值最终为b c,而不是c.

当起始值大于结束值时:

t=0.047389 b=225.000000 c=110.000000 d=700.000000 value=225.123520
t=0.095008 b=225.000000 c=110.000000 d=700.000000 value=225.496460
...
t=0.904504 b=225.000000 c=110.000000 d=700.000000 value=334.498413
t=0.952122 b=225.000000 c=110.000000 d=700.000000 value=334.873932
t=0.999740 b=225.000000 c=110.000000 d=700.000000 value=335.000000

同样,100%的值最终为b c,而不是c.

也许我已经破坏了代码.我怎样才能实现适当的轻松自拔?

最佳答案 有点晚了,但现在很快.

在Playgrounds中像这样运行:

import UIKit

var sValue: CGFloat = 0
var eValue: CGFloat = 100
var tDuration: CGFloat = 100
var eTime: CGFloat = 0

var currentValue: CGFloat = 0

func easeInOutQuad (percentComplete: CGFloat, elapsedTimeMs: CGFloat, startValue: CGFloat, endValue: CGFloat, totalDuration: CGFloat) -> CGFloat {
    var newElapsedTimeMs = elapsedTimeMs
    newElapsedTimeMs /= totalDuration/2

    if newElapsedTimeMs < 1 {
        return endValue/2*newElapsedTimeMs*newElapsedTimeMs + startValue
    }
    newElapsedTimeMs = newElapsedTimeMs - 1
    return -endValue/2 * ((newElapsedTimeMs)*(newElapsedTimeMs-2) - 1) + startValue
}

for i in eTime...tDuration {
    currentValue = easeInOutQuad(0, CGFloat(i), sValue, eValue, tDuration)
}
点赞