【iOS】用Layer创建一个三维模型以及拖动
关于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创建一个三维模型以及拖动的更多相关文章
- iOS中如何创建一个滑出式导航面板(1)
本文将介绍如何创建类似Facebook和Path iOS程序中的滑出式导航面板. 向右滑动 滑出式设计模式可以让开发者在程序中添加常用的导航功能,又不会浪费屏幕上宝贵的空间.用户可以在任意时间滑出导航 ...
- 在iOS 4中创建一个LDGradientView样式的渐变视图
本教程将演示如何在 Swift 4 中创建一个多功能的.@IBDesignable 样式的渐变视图类.你可以将 CAGradientView 放到 storyboard 中,并在设计时预览,或者以编程 ...
- Delphi for iOS开发指南(3):创建一个FireMonkey iOS应用程序
http://cache.baiducontent.com/c?m=9d78d513d9d431a94f9d92697d60c015134381132ba1d0020fa48449e3732b4b50 ...
- IOS开发之小实例--使用UIImagePickerController创建一个简单的相机应用程序
前言:本篇博文是本人阅读国外的IOS Programming Tutorial的一篇入门文章的学习过程总结,难度不大,因为是入门.主要是入门UIImagePickerController这个控制器,那 ...
- 如何用 React Native 创建一个iOS APP?(三)
前两部分,<如何用 React Native 创建一个iOS APP?>,<如何用 React Native 创建一个iOS APP (二)?>中,我们分别讲了用 React ...
- 如何用 React Native 创建一个iOS APP?(二)
我们书接上文<如何用 React Native 创建一个iOS APP?>,继续来讲如何用 React Native 创建一个iOS APP.接下来,我们会涉及到很多控件. 1 AppRe ...
- 如何用 React Native 创建一个iOS APP?
诚然,React Native 结合了 Web 应用和 Native 应用的优势,可以使用 JavaScript 来开发 iOS 和 Android 原生应用.在 JavaScript 中用 Reac ...
- 【iOS开展-50】使用它来创建一个新的类的实现代码包,因此,不自觉地练习简单MVC实验,附带动画
接下来说说代码封装最后一个个案. 最后一种情况看:[iOS开展-48]九宫格案例:自己主动布局.字典转模型运用.id和instancetype差别.xib反复视图运用及与nib关系 (1)代码封装的原 ...
- 【转】ios tableView那些事(一)创建一个简单的tableView
工作也有半年多了!几乎每个项目中的会用到tableview这个神奇而好用的控件,在学习和工作中都会看别人的博客!对我有很大的帮助,就如同站在巨人的肩膀上的感觉吧 哈哈!于是决定重新开始写博客,希望能帮 ...
随机推荐
- Android开发小记
一,下载解压adt-bundle,直接可以用来开发了二,新建android项目时不勾选创建activity,来看看如何手动创建activity1,在空项目添加class文件,选择超类为activity ...
- js遍历对象的属性并且动态添加属性
var person= { name: 'zhangsan', pass: '123' , 'sni.ni' : 'sss', hello:function (){ for(var i=0;i< ...
- CSS+DIV标签命名规范 搜索引擎最喜欢
搜索引擎优化(seo)有很多工作要做,其中对代码的优化是一个很关键的步骤.为了更加符合SEO的规范,下面是目前流行的CSS+DIV的命名规则: 登录条:loginBar 标志:logo 侧栏:si ...
- 信号量多-threaded同步Semaphore
Semaphore它是JDK1.5一个实现后,外面有个办法同步.Semaphore能够保持其当前的线程接入号码.并提供了一个同步机制. 采用Semaphore时,可以用相同的对资源的访问进行控制的线程 ...
- nvarchar and nchar
Same: 1.store unicode charactor. 2. A charactor is stroed with 2 bytes. Different. 1. nchar 会自动填充数据 ...
- 关于PagedDataSource分页属性与DataSet和DataTable详解
Asp.net提供了三个功能强大的列表控件:DataGrid.DataList和Repeater控件,但其中只有DataGrid控件提供分页功能.相对DataGrid,DataList和Repeate ...
- C# 模拟提交带附件(input type=file)的表单
今天调用某API时,对于文档中的传入参数:File[] 类型,感觉很陌生,无从下手! 按通常的方式在json参数中加入file的二进制数据提交,一直报错(参数错误)!后来经过多方咨询,是要换一种 表单 ...
- JavaScript基础知识----基本语法
JavaScript 语句 JavaScript 语句向浏览器发出的命令.语句的作用是告诉浏览器该做什么. 分号 ; 分号用于分隔 JavaScript 语句. 通常我们在每条可执行的语句结尾添加分号 ...
- js 从一个json拼接成另一个json,并做json数据分页table展示
先给数据: //原始json数据json = [{"id":"1","aid":"013","performa ...
- ipa制作
打包ipa步骤: 项目名称 -> edit scheme -> 如图选择release 点击close后,选择真机 然后command+b编译程序,右击app,show in Finder ...