1. CATransform3D結構成員的意義。
struct CATransform3D { CGFloat m11(x縮放), m12(y切變), m13(旋轉), m14(); CGFloat m21(x切變), m22(y縮放), m23(), m24(); CGFloat m31(旋轉), m32(), m33(), m34(透視效果,要操作的這個對象要有旋轉的角度,否則沒有效果。正直/負值都有意義); CGFloat m41(x平移), m42(y平移), m43(z平移), m44(); };
ps:整體比例變換時,也就是m11==m22時,若m33>1,圖形整體縮小,若0<m33<1,圖形整體放大,若s<0,發生關於原點的對稱等比變換。
()空的地方以后補充。
2. CATransform3DMakeTranslation
CATransform3DMakeTranslation(0, 0, 0) 創建了一個4*4的單位矩陣。
3. CATransform3DMakeRotation And CATransform3DRotate
CATransform3DMakeRotation()
_transformedLayer = [CALayer layer]; _transformedLayer.frame = self.bounds; _transformedLayer.anchorPoint = CGPointMake(0.5f, 0.5f); CATransform3D sublayerTransform = CATransform3DIdentity; // Set perspective sublayerTransform.m34 = kPerspective; [_transformedLayer setSublayerTransform:sublayerTransform]; [self.layer addSublayer:_transformedLayer]; //init Sublayers CATransform3D t = CATransform3DMakeTranslation(0, 0, 0); // take snapshot of the current view [_transformedLayer addSublayer:[self snapshot:t withView:_contentView isMasked:YES]]; // 暫時先支持一個方向翻轉 RotateDirection direction = RotateFromBottom; if (YES || direction == RotateFromBottom) { CGFloat height = self.bounds.size.height; //CGFloat cubeSize = 100.0f; t = CATransform3DRotate(t, D2R(90.0), 1, 0, 0);【1】 t = CATransform3DTranslate(t, 0, height, 0); CALayer *subLayer = [self snapshot:t withView:view isMasked:YES]; [_transformedLayer addSublayer:subLayer]; } else { } _newContentView = view; [self animationCubeRotate:direction withDuration:duration];
4. 翻轉的動畫
- (void)animationCubeRotate:(RotateDirection)direction withDuration:(float)duration { [CATransaction flush]; CGFloat height = self.bounds.size.height; CABasicAnimation *rotation; // CABasicAnimation *translationX; // 如果沿X軸翻轉,則用不到這個變量. CABasicAnimation *translationY; // 如果沿Y軸翻轉,則用不到這個變量. CABasicAnimation *translationZ; CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; animationGroup.delegate = self; animationGroup.duration = duration; if ( direction == RotateFromBottom ) { // 創建(某方向)關鍵幀動畫. translationY = [CABasicAnimation animationWithKeyPath: @"sublayerTransform.translation.y"]; translationY.toValue = [NSNumber numberWithFloat:-(height / 2)];【2】 rotation = [CABasicAnimation animationWithKeyPath: @"sublayerTransform.rotation.x"]; rotation.toValue = [NSNumber numberWithFloat:D2R(-90.0f)]; } else if ( direction == RotateFromTop ) { } // 處理Z軸 translationZ = [CABasicAnimation animationWithKeyPath: @"sublayerTransform.translation.z"]; translationZ.toValue = [NSNumber numberWithFloat:height / 2];【3】 animationGroup.animations = [NSArray arrayWithObjects: rotation, translationY, translationZ, nil]; animationGroup.fillMode = kCAFillModeForwards; animationGroup.removedOnCompletion = NO; [_transformedLayer addAnimation:animationGroup forKey:kAnimationKey]; }
made, 我發現這個東西確實很難講清楚,主要是因為我理論薄弱,
【1】針對X軸旋轉,就是1,0,0,針對Y軸旋轉,就是0,1,0...下面那行也要進行正確的轉換。
【2】此處應該是和 anchorPoint有關系的。
【3】這個值會影響類似於深度的東西,比如說Cube會離我們更近,或者是更遠。(但是,似乎不算是透視關系)