java 三维旋转立方体_iOS-从三维立方体到理解CATransform3D&CGAffineTransform&m34

前言

在写Custom Layout的demo时,用到了CATransform3D的m34参数,不务正业的想探究下这个矩阵到底为什么能影响到图形的透视旋转等等变换,所以通过本篇文章总结一下收获,供以后参考

目录

简单实现三维立方体

CATransform3D&CGAffineTransform使用介绍

原理探究及理解

简单实现三维立方体

fac43b0275d73b9a7c3abe82210deb15.gif

实现这个蛮简单的,只需要合理的调整旋转角度和平移,再加上一个定时器,完美(显然这个效果没什么卵用,但是笔者这只鶸来说,刚蹦出来的时候还是蛮开心的)

实现步骤 : 1.定义一个Basic View -> 2.调整并添加立方体的六个面 3.定时器调整Basic View的layer旋转

特别注意:旋转时,我们需要同时作用于6个子layer,所以请留意self.animateCube.layer.sublayerTransform = transform中使用的是sublayerTransform而非transform

CGRect targetBounds = (CGRect){CGPointZero,CGSizeMake(200, 200)};

self.animateCube = [[UIView alloc] initWithFrame:targetBounds];

_animateCube.center = self.view.center;

[self.view addSubview:self.animateCube];

UIView *test = [[UIView alloc] initWithFrame:targetBounds];// front

test.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.25];

test.layer.transform = CATransform3DTranslate(test.layer.transform, 0, 0, 100);

UIView *test1 = [[UIView alloc] initWithFrame:targetBounds];// back

test1.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];

test1.layer.transform = CATransform3DTranslate(test1.layer.transform, 0, 0, -100);

UIView *test2 = [[UIView alloc] initWithFrame:targetBounds];// left

test2.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.5];

test2.layer.transform = CATransform3DTranslate(test2.layer.transform, -100, 0, 0);

test2.layer.transform = CATransform3DRotate(test2.layer.transform, M_PI_2, 0, 1, 0);

UIView *test3 = [[UIView alloc] initWithFrame:targetBounds];// right

test3.backgroundColor = [[UIColor purpleColor] colorWithAlphaComponent:0.5];

test3.layer.transform = CATransform3DTranslate(test3.layer.transform, 100, 0, 0);

test3.layer.transform = CATransform3DRotate(test3.layer.transform, M_PI_2, 0, 1, 0);

UIView *test4 = [[UIView alloc] initWithFrame:targetBounds];// head

test4.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.5];

test4.layer.transform = CATransform3DTranslate(test4.layer.transform, 0, 100, 0);

test4.layer.transform = CATransform3DRotate(test4.layer.transform, M_PI_2, 1, 0, 0);

UIView *test5 = [[UIView alloc] initWithFrame:targetBounds];// foot

test5.backgroundColor = [[UIColor greenColor] col

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值