好尴尬,按照套路,我点进了CATransformLayer.h,但是空空如也,什么也没有。那么CATransfromLayer的作用是啥呢?只能通过撸代码来体会了。
//先上效果图吧
对比效果吧
部分代码
绘制立方体
//面1
transfrom = CATransform3DMakeTranslation(0, 0, sub_width/2);
[cube addSublayer:[self layerWithTransform:transfrom]];
//面2
transfrom = CATransform3DMakeTranslation(0, 0, -sub_width/2);
[cube addSublayer:[self layerWithTransform:transfrom]];
//面3
transfrom = CATransform3DMakeTranslation(sub_width/2, 0, 0);
transfrom = CATransform3DRotate(transfrom, M_PI_2, 0, 1, 0);
[cube addSublayer:[self layerWithTransform:transfrom]];
//面4
transfrom = CATransform3DMakeTranslation(-sub_width/2, 0, 0);
transfrom = CATransform3DRotate(transfrom, -M_PI_2, 0, 1, 0);
[cube addSublayer:[self layerWithTransform:transfrom]];
//面5
transfrom = CATransform3DMakeTranslation(0, sub_width/2, 0);
transfrom = CATransform3DRotate(transfrom, M_PI_2, 1, 0, 0);
[cube addSublayer:[self layerWithTransform:transfrom]];
//面6
transfrom = CATransform3DMakeTranslation(0, -sub_width/2, 0);
transfrom = CATransform3DRotate(transfrom, -M_PI_2, 1, 0, 0);
[cube addSublayer:[self layerWithTransform:transfrom]];
动画
static float degree = 0.f;
CGFloat duration = 1.f;
CGFloat singleDuration = 1 / (duration * 60.0);
CGFloat singleDegree = 45/(duration * 60.0);
CATransform3D fromValue = CATransform3DIdentity;
fromValue.m34 = -1/500;
fromValue = CATransform3DRotate(fromValue, DEGREE(degree), 0, 1, 0);
CATransform3D toValue = CATransform3DIdentity;
toValue.m34 = -1/500;
toValue = CATransform3DRotate(toValue, DEGREE(degree += singleDegree), 0, 1, 0);
//使用transform无法实现旋转2*M_PI,此时可以使用虚拟属性@"transform.rotation.y",可以使用相对值旋转byValue
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.duration = singleDuration;
animation.fromValue = [NSValue valueWithCATransform3D:fromValue];
animation.toValue = [NSValue valueWithCATransform3D:toValue];
animation.repeatCount = HUGE_VALL;
animation.removedOnCompletion = NO;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[layer addAnimation:animation forKey:@"transform"];
网友评论