導(dǎo)語(yǔ):本文將依據(jù)http://daneden.github.io/animate.css/中的css動(dòng)畫(huà),將css動(dòng)畫(huà)轉(zhuǎn)換為ios的動(dòng)畫(huà)。
1.bounce 彈跳動(dòng)畫(huà)
先來(lái)看下css3動(dòng)畫(huà)的代碼
@-webkit-keyframes bounce {
0%, 20%, 53%, 80%, 100% {
-webkit-transition-timing-function: cubic-bezier(0.215, 0.610, 0.355, 1.000);
transition-timing-function: cubic-bezier(0.215, 0.610, 0.355, 1.000);
-webkit-transform: translate3d(0,0,0);
transform: translate3d(0,0,0);
}
40%, 43% {
-webkit-transition-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060);
transition-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060);
-webkit-transform: translate3d(0, -30px, 0);
transform: translate3d(0, -30px, 0);
}
70% {
-webkit-transition-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060);
transition-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060);
-webkit-transform: translate3d(0, -15px, 0);
transform: translate3d(0, -15px, 0);
}
90% {
-webkit-transform: translate3d(0,-4px,0);
transform: translate3d(0,-4px,0);
}
}
可以看到,css主要使用了translate3d 和 transition-timing-function 屬性。
在ios 的core animation 中存在相同的屬性可以達(dá)到相同的效果,可以使用
CAKeyframeAnimation 關(guān)鍵幀動(dòng)畫(huà)。
附上OC版代碼
- (void)bounce{
CAKeyframeAnimation *keyAnim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
CATransform3D transform3D_1= CATransform3DMakeTranslation( 0, 0 , 0);
CATransform3D transform3D_2= CATransform3DMakeTranslation( 0, -15, 0);
CATransform3D transform3D_3= CATransform3DMakeTranslation( 0, -30, 0);
CATransform3D transform3D_4= CATransform3DMakeTranslation( 0, -4, 0);
NSMutableArray *transform3Ds = [NSMutableArray array];
[transform3Ds addObject:[self getValueWithCATransform3D:transform3D_1]];
[transform3Ds addObject:[self getValueWithCATransform3D:transform3D_1]];
[transform3Ds addObject:[self getValueWithCATransform3D:transform3D_3]];
[transform3Ds addObject:[self getValueWithCATransform3D:transform3D_3]];
[transform3Ds addObject:[self getValueWithCATransform3D:transform3D_1]];
[transform3Ds addObject:[self getValueWithCATransform3D:transform3D_2]];
[transform3Ds addObject:[self getValueWithCATransform3D:transform3D_1]];
[transform3Ds addObject:[self getValueWithCATransform3D:transform3D_4]];
[transform3Ds addObject:[self getValueWithCATransform3D:transform3D_1]];
[keyAnim setValues:transform3Ds];
NSMutableArray *keyTimes = [NSMutableArray array];
[keyTimes addObject:[self getNumberWithFloat:0]];
[keyTimes addObject:[self getNumberWithFloat:.2]];
[keyTimes addObject:[self getNumberWithFloat:.4]];
[keyTimes addObject:[self getNumberWithFloat:.43]];
[keyTimes addObject:[self getNumberWithFloat:.53]];
[keyTimes addObject:[self getNumberWithFloat:.7]];
[keyTimes addObject:[self getNumberWithFloat:.8]];
[keyTimes addObject:[self getNumberWithFloat:.9]];
[keyTimes addObject:[self getNumberWithFloat:1.0]];
[keyAnim setKeyTimes:keyTimes];
//設(shè)置每個(gè)關(guān)鍵位置速度的變化
NSMutableArray *timings = [NSMutableArray array];
[timings addObject:[self getFunctionWithControlPoints:0.215 :0.610 :0.355 :1]];
[timings addObject:[self getFunctionWithControlPoints:0.215 :0.610 :0.355 :1]];
[timings addObject:[self getFunctionWithControlPoints:0.755 :0.050 :0.855 :0.060]];
[timings addObject:[self getFunctionWithControlPoints:0.755 :0.050 :0.855 :0.060]];
[timings addObject:[self getFunctionWithControlPoints:0.215 :0.610 :0.355 :1]];
[timings addObject:[self getFunctionWithControlPoints:0.755 :0.050 :0.855 :0.060]];
[timings addObject:[self getFunctionWithControlPoints:0.215 :0.610 :0.355 :1]];
[timings addObject:[self getFunctionWithControlPoints:0 :1 :0 :1]];
[timings addObject:[self getFunctionWithControlPoints:0.215 :0.610 :0.355 :1]];
[keyAnim setTimingFunctions:timings];
[keyAnim setDuration:1];
[keyAnim setCalculationMode:kCAAnimationCubic];
[keyAnim setFillMode:kCAFillModeForwards];
[keyAnim setRemovedOnCompletion:NO];
[self.TextTest.layer addAnimation:keyAnim forKey:nil];
}
-(NSValue *)getValueWithCATransform3D:(CATransform3D)t{
return [NSValue valueWithCATransform3D:t];
}
-(NSNumber *)getNumberWithFloat:(float)num
{
return [NSNumber numberWithFloat:num];
}
- (CAMediaTimingFunction*)getFunctionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y{
return [CAMediaTimingFunction functionWithControlPoints:c1x :c1y :c2x :c2y];
}
gif 效果圖