好尷尬恋腕,按照套路腺办,我點進(jìn)了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無法實現(xiàn)旋轉(zhuǎn)2*M_PI躬拢,此時可以使用虛擬屬性@"transform.rotation.y",可以使用相對值旋轉(zhuǎn)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"];