CATransform3D-Cube

原文已找不到


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会离我们更近,或者是更远。(但是,似乎不算是透视关系)

你可能感兴趣的:(animation,float,图形,layer)