关于CATransform3D矩阵变换的简单解析
效果图:
我能能够用上的CATransform3D其实很简单,并不复杂.
CATransform3D有着4种东西我们可以设置.
1. 透视效果(由m34的值决定)
2. 位移变换(主要是x,y方向)
3. 缩放变换
4. 空间旋转
源码:
//
// RootViewController.m
//
// Copyright (c) 2014年 Y.X. All rights reserved.
//
#import "RootViewController.h"
#import "CATransform3DOperation.h"
#import "YXGCD.h"
@interface RootViewController ()
@property (nonatomic, strong) GCDTimer *timer;
@end
// 将角度转换为弧度
#define DEGREES_TO_RADIANS(d) ((d) * M_PI / 180.f)
@implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
// 给Layer一个图片内容
UIImage *image = [UIImage imageNamed:@"4"];
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
layer.position = self.view.center;
layer.borderWidth = 4.f;
layer.borderColor = [UIColor redColor].CGColor;
layer.contents = (__bridge id)image.CGImage;
[self.view.layer addSublayer:layer];
// 6s后执行操作
[[GCDQueue mainQueue] execute:^{
// 初始化3D变换,获取默认值
CATransform3D perspectiveTransform = CATransform3DIdentity;
// 透视
perspectiveTransform.m34 = -1.0/500.0;
// 位移
perspectiveTransform = CATransform3DTranslate(perspectiveTransform, 30, -35, 0);
// 空间旋转
perspectiveTransform = CATransform3DRotate(perspectiveTransform,
DEGREES_TO_RADIANS(30), .75, 1, -0.5);
// 缩放变换
perspectiveTransform = CATransform3DScale(perspectiveTransform, 0.75, 0.75, 0.75);
layer.transform = perspectiveTransform;
layer.speed = 0.5;
} afterDelay:NSEC_PER_SEC * 6];
// 9s后执行操作
[[GCDQueue mainQueue] execute:^{
// 初始化3D变换,获取默认值
CATransform3D perspectiveTransform = CATransform3DIdentity;
layer.transform = perspectiveTransform;
layer.speed = 0.5;
} afterDelay:NSEC_PER_SEC * 9];
}
@end
核心代码:
http://stackoverflow.com/questions/3881446/meaning-of-m34-of-catransform3d