【cocos2d-iphone】CCLayerColor 设置圆角 ,CCLayerColor Rounded Rectangle Drawing
来源:互联网 发布:linux 壁纸 编辑:程序博客网 时间:2024/05/16 01:39
转载 需注明本blog 地址
新建一个CCLayerColor 的子类,继承 -(void)draw 方法 不需要调用 [super draw]
给CCLayerColor设置圆角
CCLayerColor Rounded Rectangle Drawing
/* ======================================================================================== = Add these methods to a file that gets included in your .pch or add these directly to = = CCDrawingPrimitives.h and .m = ==========================================================================================*//** * Draws a rounded rectangle. * * @param rect The rectangle to draw. * @param cornerRadius The radius of the corners. * @param smoothness A multiplier for the number of segments drawn for each corner. 2 or 3 is recommended. * @param cornersToRound An array of BOOL values that signifies which corners we should round. The indices * represent each corner as follows: 0 -> bottomLeft, 1 -> topLeft, 2 -> topRight, 3 -> bottomRight. */void ccDrawSolidRoundedRect(CGRect rect, int cornerRadius, int smoothness, BOOL *cornersToRound) { CGPoint origin = rect.origin; CGSize size = rect.size; // number of segments for each rounded corner, +1 for end point of the curve int roundedCornerSegments = cornerRadius * smoothness + 1; // get the total vertices we'll need to draw int totalVertices = 0; for (int i = 0; i < 4; i++) { // add number of vertices needed for a rounded corner if we're rounding it, // else just add 1 vertex for the corner if (cornersToRound[i]) { totalVertices += roundedCornerSegments; } else { totalVertices += 1; } } ccVertex2F vertices[totalVertices]; // create the vertices we're going to draw in clockwise fashion starting from bottom left corner int currentVertexIndex = 0; for (int i = 0; i < 4; i++) { // if we don't want to draw the rounded corner just add a vertex at the corner point if (!cornersToRound[i]) { switch (i) { case 0: vertices[currentVertexIndex] = (ccVertex2F){origin.x, origin.y}; break; case 1: vertices[currentVertexIndex] = (ccVertex2F){origin.x, origin.y + size.height}; break; case 2: vertices[currentVertexIndex] = (ccVertex2F){origin.x + size.width, origin.y + size.height}; break; case 3: vertices[currentVertexIndex] = (ccVertex2F){origin.x + size.width, origin.y}; break; default: break; } currentVertexIndex++; } else { // we want the corner rounded so add vertices for that rounded corner switch (i) { case 0: addCubicBezierVertices(ccp(origin.x + cornerRadius, origin.y), ccp(origin.x + cornerRadius/2, origin.y), ccp(origin.x, origin.y + cornerRadius/2), ccp(origin.x, origin.y + cornerRadius), roundedCornerSegments, &vertices[currentVertexIndex]); break; case 1: addCubicBezierVertices(ccp(origin.x, origin.y + size.height - cornerRadius), ccp(origin.x, origin.y + size.height - cornerRadius/2), ccp(origin.x + cornerRadius/2, origin.y + size.height), ccp(origin.x + cornerRadius, origin.y + size.height), roundedCornerSegments, &vertices[currentVertexIndex]); break; case 2: addCubicBezierVertices(ccp(origin.x + size.width - cornerRadius, origin.y + size.height), ccp(origin.x + size.width - cornerRadius/2, origin.y + size.height), ccp(origin.x + size.width, origin.y + size.height - cornerRadius/2), ccp(origin.x + size.width, origin.y + size.height - cornerRadius), roundedCornerSegments, &vertices[currentVertexIndex]); break; case 3: addCubicBezierVertices(ccp(origin.x + size.width, origin.y + cornerRadius), ccp(origin.x + size.width, origin.y + cornerRadius/2), ccp(origin.x + size.width - cornerRadius/2, origin.y), ccp(origin.x + size.width - cornerRadius, origin.y), roundedCornerSegments, &vertices[currentVertexIndex]); break; default: break; } currentVertexIndex += roundedCornerSegments; } } // we now have all the vertices we need so draw themccDrawVerticiesFilled(vertices, totalVertices);}/** * Adds vertices for a bezier curve with the given parameters to an array of vertices. It uses the same * implementation as ccDrawCubicBezier in CCDrawingPrimitives for vertex creation. * * @param origin Starting point for our curve. * @param control1 Control point for bending the curve before the halfway point. * @param control2 Control point for bending the curve after the halfwar point. * @param destination Ending point for our curve. * @param segments Number of segments we want in the curve. * @param vertices Pointer to an array of vertices that we'll be adding the curve vertices to. */void addCubicBezierVertices(CGPoint origin, CGPoint control1, CGPoint control2, CGPoint destination, NSUInteger segments, ccVertex2F *vertices){ int totalSegments = segments - 1;float t = 0;for(NSUInteger i = 0; i < totalSegments; i++){ ccVertex2F vertex;vertex.x = powf(1 - t, 3) * origin.x + 3.0f * powf(1 - t, 2) * t * control1.x + 3.0f * (1 - t) * t * t * control2.x + t * t * t * destination.x;vertex.y = powf(1 - t, 3) * origin.y + 3.0f * powf(1 - t, 2) * t * control1.y + 3.0f * (1 - t) * t * t * control2.y + t * t * t * destination.y; vertices[i] = vertex;t += 1.0f / totalSegments;} ccVertex2F finalVertex = (ccVertex2F) {destination.x, destination.y};vertices[totalSegments] = finalVertex;}/* ==================================================================== = Add this method to CCDrawingPrimitives.m and the header in the .h = = NOTE: If you update your cocos2d, you could lose this code = ======================================================================*//** * Draws the given vertices and fills in the inside. * * @param vertices Array of ccVertex2F to draw. * @param numberOfPoints Number of vertices in the array. */void ccDrawVerticiesFilled( const ccVertex2F *vertices, NSUInteger numberOfPoints){lazy_init(); [shader_ use];[shader_ setUniformsForBuiltins];[shader_ setUniformLocation:colorLocation_ with4fv:(GLfloat*) &color_.r count:1]; ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices); glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) numberOfPoints);}
新建一个CCLayerColor 的子类,继承 -(void)draw 方法 不需要调用 [super draw]
example code
- (void)draw { float width = self.contentSize.width; float height = self.contentSize.height; int cornerRadius = 8; int smoothness = 2; BOOL cornersToRound[4] = { YES, YES, YES, YES }; ccDrawColor4F(0.2, 0.2, 0.2, 1); ccDrawSolidRoundedRect(CGRectMake(0, 0, width, height), cornerRadius, smoothness, cornersToRound);}
转载 需注明本blog 地址
- 【cocos2d-iphone】CCLayerColor 设置圆角 ,CCLayerColor Rounded Rectangle Drawing
- 用CCLayerColor设置背景颜色
- cocos2dx CCLayerColor和CCLayerColor
- Cocos2d-x结构学习(十六)CCLayerColor、CCProgressTimer、CCProgressTo、CCSet
- Cocos2d-2.x_CCLayer,CCLayerColor,CCLayerGradient,CCLayerMultiplex层的介绍
- cocos2d-x(4):CCLayer(层) CCLayerColor、CCLayerGradient、CClayerDelegate学习
- cocos2dx教程之CCLayerColor
- CCLayerColor层的使用
- cocos2dx之CCLayerColor、CCLayerGradient
- CCLayerColor 使用记录
- Cocos2d-x教程(11)-利用遮罩(蒙版)CCLayerColor制作新手引导界面(上)
- Cocos2d-x教程(12)-利用遮罩(蒙版)CCLayerColor制作新手引导界面(中)
- CCLayerColor和CCClippingNode组合新手引导功能---cocos2d-x学习之路[4]
- Cocos2d-x教程(11)-利用遮罩(蒙版)CCLayerColor制作新手引导界面(上)
- Cocos2d-x教程(12)-利用遮罩(蒙版)CCLayerColor制作新手引导界面(中)
- 总结Cocos2d-x中Layer的使用(CCLayerColor、CCLayerGradient、CCLayerMultplex)
- 我的Cocos2d-x学习笔记(五)CCLayer、CCLayerColor、CCLayerGradient、CCLayerMultiplex
- HTML5 Canvas自定义圆角矩形与虚线(Rounded Rectangle and Dash Line)
- CCProxy 代理 配置
- 如何获得设备分辨率使用命令行
- LibSVM简易入门
- js根据生日计算周岁 以及Date的相关用法
- StringBuffer 和 String Builder的区别
- 【cocos2d-iphone】CCLayerColor 设置圆角 ,CCLayerColor Rounded Rectangle Drawing
- 反射机制剖析(一):谈谈工厂家族
- ubuntu下minicom和USB转串口(转)
- centos上安装opencv库
- 在你的app中用谷歌解析器
- Ubuntu12.04 SQLite安装
- ubuntu下安装Apache+PHP+Mysql
- Linux init程序分析
- svn 查看,删除用户名,密码