使用GLKit与CoreAnimation2种方式实现3D旋转盒子

经常看到前端会写出各种炫酷的盒子动画,那iOS有没有实现方式呢?答案当然是肯定的,而且方式还有很多种。今天我就用两种方式来实现一个炫酷的旋转盒子,话不多说,先来看一下动画效果图吧~


炫酷的旋转盒子

接下来说一下实现方式吧

方式一:GLKView实现

第1步:创建context,初始化上下文时设置API版本,并设置当前context

EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

[EAGLContext setCurrentContext:context];

第2步:创建GLKView并设置代理

CGRect frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.width);

 self.glkView = [[GLKView alloc] initWithFrame:frame context:context];

 self.glkView.backgroundColor = [UIColor clearColor];

 self.glkView.delegate = self;

第3步:使用深度缓存

self.glkView.drawableDepthFormat = GLKViewDrawableDepthFormat24;

glDepthRangef(1, 0);

第4步:将GLKView 添加self.view 上

[self.view addSubview:self.glkView];

第5步:获取纹理图片

NSString *imagePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"kunkun.jpg"];

UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

第6步:设置纹理参数

NSDictionary *options = @{GLKTextureLoaderOriginBottomLeft : @(YES)};

GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithCGImage:[image CGImage] options:options error:NULL];

第7步:使用baseEffect,开启光照效果,漫反射颜色,光源位置

 self.baseEffect = [[GLKBaseEffect alloc] init];

 self.baseEffect.texture2d0.name = textureInfo.name;

 self.baseEffect.texture2d0.target = textureInfo.target;

self.baseEffect.light0.enabled = YES

self.baseEffect.light0.diffuseColor = GLKVector4Make(1, 1, 1, 1);

self.baseEffect.light0.position = GLKVector4Make(-0.5, -0.5, 5, 1);

第8步: 开辟顶点数据空间(数据结构SenceVertex 大小 * 顶点个数kCoordCount)

self.vertices = malloc(sizeof(CCVertex) * kCoordCount);

self.vertices[0] = (CCVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 0, 1}};

...

开辟顶点缓存区

glGenBuffers(1, &_vertexBuffer);

glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);

GLsizeiptr bufferSizeBytes = sizeof(CCVertex) * kCoordCount;

glBufferData(GL_ARRAY_BUFFER, bufferSizeBytes, self.vertices, GL_STATIC_DRAW);

顶点数据

glEnableVertexAttribArray(GLKVertexAttribPosition);

glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(CCVertex), NULL + offsetof(CCVertex, positionCoord));

 纹理数据

 glEnableVertexAttribArray(GLKVertexAttribTexCoord0);

 glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(CCVertex), NULL + offsetof(CCVertex, textureCoord));

 法线数据

 glEnableVertexAttribArray(GLKVertexAttribNormal);

 glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(CCVertex), NULL + offsetof(CCVertex, normal));

第9步:设置旋转角度

self.angle = (self.angle + 5) % 360;

修改baseEffect.transform.modelviewMatrix

self.baseEffect.transform.modelviewMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(self.angle), 0.3, 1, 0.7);

重新渲染

 [self.glkView display];

第10步:开启深度测试

 glEnable(GL_DEPTH_TEST);

 清除颜色缓存区&深度缓存区

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 准备绘制

  [self.baseEffect prepareToDraw];

 绘图

 glDrawArrays(GL_TRIANGLES, 0, kCoordCount);

方式二:CoreAnimation实现

第1步:添加6个面父View的layer图层

CATransform3D perspective = CATransform3DIdentity;

perspective.m34= -1.0/500.0;

 perspective =CATransform3DRotate(perspective, -M_PI_4,1,0,0);

 perspective =CATransform3DRotate(perspective, -M_PI_4,0,1,0);

 self.containerView.layer.sublayerTransform = perspective;

cube face 1

CATransform3D transform = CATransform3DMakeTranslation(0, 0, 100);

 [self addFace:0 withTransform:transform];

 cube face 2

 transform =CATransform3DMakeTranslation(100, 0, 0);

 transform =CATransform3DRotate(transform,M_PI_2,0,1,0);

 [self addFace:1 withTransform:transform];

...

添加面的方法,获取face视图并将其添加到容器中

 UIView*face =self.faces[index];

 [self.containerView addSubview:face];

 将face视图放在容器的中心

 CGSize containerSize = self.containerView.bounds.size;

 face.center=CGPointMake(containerSize.width/2.0, containerSize.height/2.0);

 添加transform

 face.layer.transform= transform;

第2步:计算并旋转

self.angle = (self.angle + 5) % 360;

 floatdeg =self.angle* (M_PI/180);

 CATransform3D temp = CATransform3DIdentity;

 temp =CATransform3DRotate(temp, deg,0.3,1,0.7);

 self.containerView.layer.sublayerTransform = temp;

以上就是两种方式实现的炫酷旋转盒子~

你可能感兴趣的:(使用GLKit与CoreAnimation2种方式实现3D旋转盒子)