3 回答

TA貢獻1802條經驗 獲得超5個贊
我找到了這個:
可可與愛-核心動畫中的參數加速曲線
但是我認為可以通過使用塊使其變得更簡單,更易讀。因此,我們可以在CAKeyframeAnimation上定義一個類別,如下所示:
CAKeyframeAnimation + Parametric.h:
// this should be a function that takes a time value between
// 0.0 and 1.0 (where 0.0 is the beginning of the animation
// and 1.0 is the end) and returns a scale factor where 0.0
// would produce the starting value and 1.0 would produce the
// ending value
typedef double (^KeyframeParametricBlock)(double);
@interface CAKeyframeAnimation (Parametric)
+ (id)animationWithKeyPath:(NSString *)path
function:(KeyframeParametricBlock)block
fromValue:(double)fromValue
toValue:(double)toValue;
CAKeyframeAnimation + Parametric.m:
@implementation CAKeyframeAnimation (Parametric)
+ (id)animationWithKeyPath:(NSString *)path
function:(KeyframeParametricBlock)block
fromValue:(double)fromValue
toValue:(double)toValue {
// get a keyframe animation to set up
CAKeyframeAnimation *animation =
[CAKeyframeAnimation animationWithKeyPath:path];
// break the time into steps
// (the more steps, the smoother the animation)
NSUInteger steps = 100;
NSMutableArray *values = [NSMutableArray arrayWithCapacity:steps];
double time = 0.0;
double timeStep = 1.0 / (double)(steps - 1);
for(NSUInteger i = 0; i < steps; i++) {
double value = fromValue + (block(time) * (toValue - fromValue));
[values addObject:[NSNumber numberWithDouble:value]];
time += timeStep;
}
// we want linear animation between keyframes, with equal time steps
animation.calculationMode = kCAAnimationLinear;
// set keyframes and we're done
[animation setValues:values];
return(animation);
}
@end
現在用法將如下所示:
// define a parametric function
KeyframeParametricBlock function = ^double(double time) {
return(1.0 - pow((1.0 - time), 2.0));
};
if (layer) {
[CATransaction begin];
[CATransaction
setValue:[NSNumber numberWithFloat:2.5]
forKey:kCATransactionAnimationDuration];
// make an animation
CAAnimation *drop = [CAKeyframeAnimation
animationWithKeyPath:@"position.y"
function:function fromValue:30.0 toValue:450.0];
// use it
[layer addAnimation:drop forKey:@"position"];
[CATransaction commit];
}
我知道這可能不像您想要的那么簡單,但這只是一個開始。

TA貢獻1797條經驗 獲得超4個贊
從iOS 10開始,可以使用兩個新的計時對象更輕松地創建自定義計時功能。
1)UICubicTimingParameters允許將三次貝塞爾曲線定義為緩動函數。
let cubicTimingParameters = UICubicTimingParameters(controlPoint1: CGPoint(x: 0.25, y: 0.1), controlPoint2: CGPoint(x: 0.25, y: 1))
let animator = UIViewPropertyAnimator(duration: 0.3, timingParameters: cubicTimingParameters)
或僅在動畫設計器初始化時使用控制點
let controlPoint1 = CGPoint(x: 0.25, y: 0.1)
let controlPoint2 = CGPoint(x: 0.25, y: 1)
let animator = UIViewPropertyAnimator(duration: 0.3, controlPoint1: controlPoint1, controlPoint2: controlPoint2)?
這項很棒的服務將幫助您為曲線選擇控制點。
2)UISpringTimingParameters使開發人員可以控制阻尼比,質量,剛度和初始速度來創建所需的彈簧性能。
let velocity = CGVector(dx: 1, dy: 0)
let springParameters = UISpringTimingParameters(mass: 1.8, stiffness: 330, damping: 33, initialVelocity: velocity)
let springAnimator = UIViewPropertyAnimator(duration: 0.0, timingParameters: springParameters)
持續時間參數仍在Animator中顯示,但在春季計時中將被忽略。
如果這兩個選項還不夠,您還可以通過確認UITimingCurveProvider協議來實現自己的時序曲線。

TA貢獻1883條經驗 獲得超3個贊
創建自定義計時功能的一種方法是使用CAMediaTimingFunction中的functionWithControlPoints ::::工廠方法(也有相應的initWithControlPoints :::: init方法)。這是為您的計時功能創建貝塞爾曲線。它不是任意曲線,但貝塞爾曲線非常有力且靈活。掌握控制點需要一些實踐。提示:大多數繪圖程序都可以創建貝塞爾曲線。進行這些操作將使您對用控制點表示的曲線產生視覺反饋。
在這個鏈接指向蘋果的文檔。關于如何從曲線構造預構建函數的內容很簡短,但很有用。
編輯:以下代碼顯示了一個簡單的反彈動畫。為此,我創建了一個合成的計時函數(值和計時 NSArray屬性),并為動畫的每個片段賦予了不同的時間長度(keytimes屬性)。這樣,您可以合成貝塞爾曲線以為動畫合成更復雜的時序。這是一篇有關此類動畫的好文章,并提供了不錯的示例代碼。
- (void)viewDidLoad {
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 50.0)];
v.backgroundColor = [UIColor redColor];
CGFloat y = self.view.bounds.size.height;
v.center = CGPointMake(self.view.bounds.size.width/2.0, 50.0/2.0);
[self.view addSubview:v];
//[CATransaction begin];
CAKeyframeAnimation * animation;
animation = [CAKeyframeAnimation animationWithKeyPath:@"position.y"];
animation.duration = 3.0;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
NSMutableArray *values = [NSMutableArray array];
NSMutableArray *timings = [NSMutableArray array];
NSMutableArray *keytimes = [NSMutableArray array];
//Start
[values addObject:[NSNumber numberWithFloat:25.0]];
[timings addObject:GetTiming(kCAMediaTimingFunctionEaseIn)];
[keytimes addObject:[NSNumber numberWithFloat:0.0]];
//Drop down
[values addObject:[NSNumber numberWithFloat:y]];
[timings addObject:GetTiming(kCAMediaTimingFunctionEaseOut)];
[keytimes addObject:[NSNumber numberWithFloat:0.6]];
// bounce up
[values addObject:[NSNumber numberWithFloat:0.7 * y]];
[timings addObject:GetTiming(kCAMediaTimingFunctionEaseIn)];
[keytimes addObject:[NSNumber numberWithFloat:0.8]];
// fihish down
[values addObject:[NSNumber numberWithFloat:y]];
[keytimes addObject:[NSNumber numberWithFloat:1.0]];
//[timings addObject:GetTiming(kCAMediaTimingFunctionEaseIn)];
animation.values = values;
animation.timingFunctions = timings;
animation.keyTimes = keytimes;
[v.layer addAnimation:animation forKey:nil];
//[CATransaction commit];
}
- 3 回答
- 0 關注
- 784 瀏覽
添加回答
舉報