关于CALayer的介绍以及基本属性,在这篇博客中有交代:CoreAnimation —— CALayer

这篇博客讲述简单的通过对layer的transform属性的设置一个CATransform3D来进行自定义三维图形,甚至后续的处理。

通常简单的仿射变换我们也是通过对其的transform属性进行设置。不过这里设置的是一个3D变换类。如果线性代数很好的话,那应该能够理解内部具体做了如何的矩阵运算。

首先我子类化一个UIView对象,把图形的绘制在这个自定义View上进行。

接口方面

@property (nonatomic, assign, readonly) CGFloat side;
@property (nonatomic, assign, readonly) BOOL autoAnimate; /**
* 创建一个正方体对象
*
* @param frame 位置
* @param side 边长
* @param animate 创建后是否自动旋转展示
*
* @return 正方体对象视图
*/
+ (HRCube *)cube3DWithFrame:(CGRect)frame side:(CGFloat)side autoAnimate:(BOOL)animate;

工厂方法的实现就是快速实例化并设置属性

+ (HRCube *)cube3DWithFrame:(CGRect)frame side:(CGFloat)side autoAnimate:(BOOL)animate
{
HRCube *cube3D = [[HRCube alloc] initWithFrame:frame];
cube3D.side = side;
cube3D.autoAnimate = animate; return cube3D;
}

类别中有两个内部成员

CALayer *_cubeLayer; //main layer
GLKMatrix4 _rotMatrix;

第一个是我们需要绘制正方体的layer。

第二个是在导入GLKit框架后,从中引入的一个矩阵变换类,我们在触摸屏幕拖动时,需要使用到这个类对正方体进行旋转处理。

初始化主layer

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_cubeLayer = [CALayer layer];
_cubeLayer.frame = self.bounds;
_cubeLayer.contentsScale = [UIScreen mainScreen].scale;
}
return self;
}

正式绘制正方体

- (void)setSide:(CGFloat)side
{
_side = side; // NSLog(@"%@", NSStringFromCGRect(_cubeLayer.bounds)); //正
[self addCubeLayer:@[@0, @0, @(_side/2), @0, @0, @0, @0]];
//背
[self addCubeLayer:@[@0, @0, @(-_side/2), @(M_PI), @0, @0, @0]];
//左
[self addCubeLayer:@[@(-_side/2), @0, @0, @(-M_PI_2), @0, @1, @0]];
//右
[self addCubeLayer:@[@(_side/2), @0, @0, @(M_PI_2), @0, @1, @0]];
//上
[self addCubeLayer:@[@0, @(-_side/2), @0, @(-M_PI_2), @1, @0, @0]];
//下
[self addCubeLayer:@[@0, @(_side/2), @0, @(M_PI_2), @1, @0, @0]]; CATransform3D transform3D = CATransform3DIdentity;
transform3D.m34 = -1.0/2000;
_cubeLayer.sublayerTransform = transform3D; [self.layer addSublayer:_cubeLayer];
}

这里的m34不是星体- -,3D变形矩阵的m34通常应该设置为-1/EYE_DISTANCE,这里设置为2000已经足够好。

addCubeLayer的实现

//添加sublayers
- (void)addCubeLayer:(NSArray *)params
{
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.contentsScale = [UIScreen mainScreen].scale;
gradient.bounds = CGRectMake(0, 0, _side, _side);
gradient.position = self.center;
gradient.colors = @[(id)kGrayColor, (id)kBlackColor];
gradient.locations = @[@0, @1];
gradient.startPoint = CGPointMake(0, 0);
gradient.endPoint = CGPointMake(0, 1);
//抗锯齿
gradient.shouldRasterize = YES; CATransform3D trans3D = CATransform3DMakeTranslation([params[0] floatValue], [params[1] floatValue], [params[2] floatValue]);
CATransform3D rotate3D = CATransform3DRotate(trans3D , [params[3] floatValue], [params[4] floatValue], [params[5] floatValue], [params[6] floatValue]);
CATransform3D transform3D = rotate3D;
// CATransform3D transform3D = CATransform3DRotate(trans3D, [params[3] floatValue], [params[4] floatValue], [params[5] floatValue], [params[6] floatValue]); gradient.transform = transform3D; [_cubeLayer addSublayer:gradient];
}

这里为了更方便观察,使用的是渐变Layer,颜色为宏,可以自由修改。

注意给其抗锯齿设为了YES。通常我们还可以在整个项目的info.plist文件中设置该内容,不过设置会对整个项目的性能产生不好的影响,所以一般来说,需要的时候再设置也是一个不错的选择。

这样我们就简单的创建好了一个正方体,不过当前摆放很正,所以只能看到一个正方形平面,下面我们给他设置自动旋转后给他一个沿着坐标轴的角度偏转,再无限围着y轴转就OK了。

//添加自定义旋转展示动画
- (void)addAnimation
{
_cubeLayer.sublayerTransform = CATransform3DRotate(_cubeLayer.sublayerTransform, M_PI/9.0, 0.5, 0.5, 0.5); CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.rotation.y"];
animation.toValue = @(MAXFLOAT);
animation.duration = MAXFLOAT;
[_cubeLayer addAnimation:animation forKey:@"rotation"];
}

这时运行程序我们就能看到一个旋转的三维正方体了

下面给其扩展一个拖动收拾旋转功能

首先给view增加一个拖动收拾,然后在Action中实现

#pragma mark - PanGesture
- (void)panRotate:(UIPanGestureRecognizer *)ges
{
static CGPoint start;
if (ges.state == UIGestureRecognizerStateBegan) {
start = [ges locationInView:self];
} else if (ges.state == UIGestureRecognizerStateChanged) {
CATransform3D transform = _cubeLayer.sublayerTransform;
_rotMatrix = GLKMatrix4MakeWithArray((void *)&transform); CGPoint loc = [ges locationInView:self];
CGPoint diff = CGPointMake(start.x-loc.x, start.y-loc.y); float rotX = 1 * GLKMathDegreesToRadians(diff.y/2.0);
float rotY = -1 * GLKMathDegreesToRadians(diff.x/2.0); bool isInvertible;
GLKVector3 xAxis = GLKMatrix4MultiplyVector3(GLKMatrix4Invert(_rotMatrix, &isInvertible),
GLKVector3Make(1, 0, 0));
_rotMatrix = GLKMatrix4Rotate(_rotMatrix, rotX, xAxis.x, xAxis.y, xAxis.z);
GLKVector3 yAxis = GLKMatrix4MultiplyVector3(GLKMatrix4Invert(_rotMatrix, &isInvertible),
GLKVector3Make(0, 1, 0));
_rotMatrix = GLKMatrix4Rotate(_rotMatrix, rotY, yAxis.x, yAxis.y, yAxis.z); _cubeLayer.sublayerTransform = *((CATransform3D *)&_rotMatrix); start = loc;
}
}

经过对矩阵的实时处理后,我们可以使用拖动收拾来在视图中实时的调整3D正方体的旋转了。

Demo源码:

CSDN:点击打开链接

GitHub:Rannie / HRCube3D

喜欢的话可以去点个星:)

以上就是本篇博客全部内容,欢迎指正和交流。转载注明出处~

【iOS】用Layer创建一个三维模型以及拖动的更多相关文章

  1. iOS中如何创建一个滑出式导航面板(1)

    本文将介绍如何创建类似Facebook和Path iOS程序中的滑出式导航面板. 向右滑动 滑出式设计模式可以让开发者在程序中添加常用的导航功能,又不会浪费屏幕上宝贵的空间.用户可以在任意时间滑出导航 ...

  2. 在iOS 4中创建一个LDGradientView样式的渐变视图

    本教程将演示如何在 Swift 4 中创建一个多功能的.@IBDesignable 样式的渐变视图类.你可以将 CAGradientView 放到 storyboard 中,并在设计时预览,或者以编程 ...

  3. Delphi for iOS开发指南(3):创建一个FireMonkey iOS应用程序

    http://cache.baiducontent.com/c?m=9d78d513d9d431a94f9d92697d60c015134381132ba1d0020fa48449e3732b4b50 ...

  4. IOS开发之小实例--使用UIImagePickerController创建一个简单的相机应用程序

    前言:本篇博文是本人阅读国外的IOS Programming Tutorial的一篇入门文章的学习过程总结,难度不大,因为是入门.主要是入门UIImagePickerController这个控制器,那 ...

  5. 如何用 React Native 创建一个iOS APP?(三)

    前两部分,<如何用 React Native 创建一个iOS APP?>,<如何用 React Native 创建一个iOS APP (二)?>中,我们分别讲了用 React ...

  6. 如何用 React Native 创建一个iOS APP?(二)

    我们书接上文<如何用 React Native 创建一个iOS APP?>,继续来讲如何用 React Native 创建一个iOS APP.接下来,我们会涉及到很多控件. 1 AppRe ...

  7. 如何用 React Native 创建一个iOS APP?

    诚然,React Native 结合了 Web 应用和 Native 应用的优势,可以使用 JavaScript 来开发 iOS 和 Android 原生应用.在 JavaScript 中用 Reac ...

  8. 【iOS开展-50】使用它来创建一个新的类的实现代码包,因此,不自觉地练习简单MVC实验,附带动画

    接下来说说代码封装最后一个个案. 最后一种情况看:[iOS开展-48]九宫格案例:自己主动布局.字典转模型运用.id和instancetype差别.xib反复视图运用及与nib关系 (1)代码封装的原 ...

  9. 【转】ios tableView那些事(一)创建一个简单的tableView

    工作也有半年多了!几乎每个项目中的会用到tableview这个神奇而好用的控件,在学习和工作中都会看别人的博客!对我有很大的帮助,就如同站在巨人的肩膀上的感觉吧 哈哈!于是决定重新开始写博客,希望能帮 ...

随机推荐

  1. Failed to upgrade Oracle Cluster Registry configuration(root.sh)

        近期在给客户基于Suse 11 sp3安装Oracle 10g RAC,在安装完clusterware运行/u01/app/crs/root.sh时收到错误提示.Failed to upgra ...

  2. TCP传输连接建立与释放详解

    一直以来有许多读者朋友对TCP的传输连接建立和释放过程不是很理解,而这又是几乎网络认证中必考的知识点,包括软考.CCNA\CCNP.H3CNA\H3CNE等,为此再把笔者年度巨作,广受好评的——< ...

  3. 利用jxl读取excel合并的单元格的一个小样例

    工作中我们可能要把Excel文件的记录保存到数据库, 今天我用jxl读取Excel文件时遇到了合并格的问题,记录例如以下: 如Excel文件例如以下: watermark/2/text/aHR0cDo ...

  4. cordova安装--创建ionic项目

    1.简介ionic ionic 是一个强大的 HTML5 应用程序开发框架(HTML5 Hybrid Mobile App Framework ). 可以帮助您使用 Web 技术,比如 HTML.CS ...

  5. 什么是DNS劫持和DNS污染?

    什么是DNS劫持和DNS污染? http://blogread.cn/it/article/7758?f=weekly 说明 我们知道,某些网络运营商为了某些目的,对 DNS 进行了某些操作,导致使用 ...

  6. QT直接支持GB18030,附它对此编码的一堆相关文档描述

    http://doc.qt.io/qt-5/qtextcodec.html http://doc.qt.io/qt-5/codec-gbk.html

  7. hibernate 事务的隔离级别 5.1

    脏读不可重复读幻读可序列化(符合事务的四个特性的正常情况 ) 解释: 脏读:事务A对数据1做了更新,但是还没有来得及提交 此时事务B对数据1进行了查询获得了事务A更新后的数据, 但是事务A因为一些原因 ...

  8. python and 和 or

    在Python 中,and 和 or 执行布尔逻辑演算,如你所期待的一样.但是它们并不返回布尔值,而是返回它们实际进行比较的值之一. 例 4.15. and 介绍 >>> 'a' a ...

  9. C 语言统计关键字出现次数

    #include <stdio.h> #include <ctype.h> #include <string.h> #define NKEYS (sizeof ke ...

  10. ORACLE uuid自己主动生成主键

    -- Create table create table TECHNOLOGYCOMPANY ( ID VARCHAR2(32) default SYS_GUID() not null, FLOWID ...