經常看到前端會寫出各種炫酷的盒子動畫融蹂,那iOS有沒有實現方式呢?答案當然是肯定的弄企,而且方式還有很多種超燃。今天我就用兩種方式來實現一個炫酷的旋轉盒子,話不多說拘领,先來看一下動畫效果圖吧~
接下來說一下實現方式吧
方式一:GLKView實現
第1步:創(chuàng)建context意乓,初始化上下文時設置API版本,并設置當前context
EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
[EAGLContext setCurrentContext:context];
第2步:創(chuàng)建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}};
...
開辟頂點緩存區(qū)
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);
?清除顏色緩存區(qū)&深度緩存區(qū)
?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;
以上就是兩種方式實現的炫酷旋轉盒子~