iOS 3D立方体

立方体.gif

3D动画总结.png

ok,多了不介绍了,直接上代码,敲一遍会比看有效果的多

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@property (nonatomic, strong) UIView *containView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from
//包含所有view的视图,用来做拖拽处理,和所有子视图的移动
_containView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
_containView.center = self.view.center;
CATransform3D trans = CATransform3DIdentity;
trans.m34 = 1/ 500;
_containView.layer.transform = trans;

#pragma mark ---底部背对着我们的视图
[self.view addSubview:_containView];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view1.backgroundColor = [UIColor purpleColor];
[_containView addSubview:view1];

#pragma mark ---右侧的视图
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view2.backgroundColor = [UIColor yellowColor];
//因为旋转是根据锚点,也就是view2的中心点旋转的,所以先将视图往x轴和z轴各偏移(200 /2)
trans = CATransform3DTranslate(trans, 100, 0, 100);
//绕y轴旋转90度
trans = CATransform3DRotate(trans, M_PI_2, 0, 1, 0);

view2.layer.transform = trans;
[_containView addSubview:view2];
#pragma mark ---左侧的视图 (只需要将view2沿x轴移动-100,沿z轴移动100,然后沿)
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view3.backgroundColor = [UIColor blueColor];
//CATransform3DTranslate 和 CATransform3DMakeTranslation区别:使用make的是不对上面效果进行叠加,不使用make是对上面视图效果进行叠加
trans = CATransform3DMakeTranslation(-100, 0, 100);
trans = CATransform3DRotate(trans, M_PI_2, 0, 1, 0);

  // trans = CATransform3DTranslate(trans, 0, 0, -200);
view3.layer.transform = trans;
[_containView addSubview:view3];

UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view4.backgroundColor = [UIColor grayColor];
[_containView addSubview:view4];

trans = CATransform3DMakeTranslation(0, -100, 100);
trans = CATransform3DRotate(trans, M_PI_2, 1, 0, 0);
//    trans = CATransform3DRotate(trans, M_PI_2, 1, 0, 0);
//    trans = CATransform3DTranslate(trans, 0, 100, 100);
view4.layer.transform = trans;
//
UIView *view5 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view5.backgroundColor = [UIColor blackColor];
[_containView addSubview:view5];
trans = CATransform3DMakeTranslation(0, 100, 100);
trans = CATransform3DRotate(trans, M_PI_2, 1, 0, 0);

 // trans = CATransform3DTranslate(trans, 0, 0, -200);
view5.layer.transform = trans;

//
  UIView *view6 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view6.backgroundColor = [UIColor greenColor];
[_containView addSubview:view6];
//    trans = CATransform3DRotate(trans, M_PI_2, 0, 1, 0);
//    trans = CATransform3DTranslate(trans, -100, 0, -100);
trans = CATransform3DMakeTranslation(0, 0, 200);
view6.layer.transform = trans;

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(changeAngle:)];
[_containView addGestureRecognizer:pan];

}

-(void)changeAngle:(UIPanGestureRecognizer *)sender{
//返回视图拖动的像素
   CGPoint point =  [sender translationInView:_containView];
//point.x/30可以决定拖动的时候旋转的速度
CGFloat angel = -M_PI_4 + point.x/30;
CGFloat angel2 = -M_PI_4 - point.y/ 30 ;

CATransform3D tran = CATransform3DIdentity;
tran.m34 = -1/ 500;
tran = CATransform3DRotate(tran, angel, 0, 1, 0);
tran = CATransform3DRotate(tran, angel2, 1, 0, 0);
// sublayerTransform: 它能够影响到它所有的直接子图层. 就是说可以一次性对这些图层的容器做变换, 然后所有的子图层都集成这个变换方法.
_containView.layer.sublayerTransform = tran;

}

注释掉的3d变化方法整体也可以构造立方体
demo + iOS动画总结+思维导图软件

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,037评论 25 709
  • 如果在一个黎明 你听到几声驼铃 意味着我将远行 好在雾气蒙蒙 掩盖了我不舍的表情 背着轻轻的行囊 带上长长的梦想 ...
    苏小呆阅读 3,184评论 1 3
  • 晨跑中,总是能够看到不一样的景象,能够让自己从中完全静静的享受这一刻身心的独处,思想的独处。 新年的开工...
    荏时光倒影阅读 3,365评论 0 1
  • 生活。生容易,活着难。 小时候最爱看的就是武侠片,例如《天龙八部》。《倚天屠龙记》,《雪花...
    卢屹阅读 2,828评论 7 3
  • 【丹昏】绝配 1 朴志训有个毛病。 心里想的和说就来的不一样,内心明明在吐槽别人,脸上还要笑着和别人说话,喜欢总说...
    RabbitMango阅读 6,343评论 2 4

友情链接更多精彩内容