开发笔记6-Graphics学习-Drawing Lines、Drawing Rectangles

来源:互联网 发布:男人花网络女主播是谁 编辑:程序博客网 时间:2024/06/05 09:49

今天上午参加了gameloft公司C++的笔试,与自己预想的地方还挺多的,第一我本以为有很多人参加这个笔试,结果就我一个人参加这个笔试,C++冷门了吗?第二笔试题全部是英文出题,要求使用英语或法语回答,如果实在说不清的话可以使用中文,冲着最后一条,我全部用了中文答题,我还纳闷居然可以用法语答题,我有些小意外,回来和同学聊一查gameloft居然是法国的公司,OMG~

今天可以透露一下gameloft的笔试题,我也就说说方向,毕竟我的英语不好,这满篇的英文我也只能半猜带蒙了,有关计算优化的,比如乘法、除法、求余运算,应该就是用些移位操作和其他的操作,之前确实没有接触过,我就知道switch()的效率比if()高,运算的优化我就知道比如*4就是左移2位,其他的不知道;考了些virtual函数实现多态的知识点,这个前些天复习过了,感觉没问题。写程序题有字符串处理的,颜色计算灰度还是什么的,最后是个求一个数是不是质数的。上机题就是个俄罗斯方块,不过有很多问题,要求你去改动完成设计目标,这个我当时就蒙了,就把其中第一问让方块落下时在屏幕中间该对了,其他的检测方块的边界变化呀出界呀我都不知道到哪找,还有内存泄露问题让你改,看了看题再看时间差不多了两个小时了,把试卷交了就回家吧。如果真的是搞C++的,题真的不难,这是我的感觉,试题是06年的,也就是说gameloft6年都没有改过题吧~~

扯完犊子,来今天学的

使用Core Graphics在屏幕上划出直线和矩形,先给出代码,来自《iOS5 Programming CookBook》有小改动

    [[UIColor blueColor] set];        CGContextRef currentContext = UIGraphicsGetCurrentContext();        CGContextSetLineWidth(currentContext, 5.0f);        CGContextMoveToPoint(currentContext, 50.0f, 10.0f);        CGContextAddLineToPoint(currentContext, 100.0f, 200.0f);        CGContextAddLineToPoint(currentContext, 300.0f, 300.0f);        CGContextAddLineToPoint(currentContext, 300.0f, 480.0f);        CGContextSetLineJoin(currentContext, kCGLineJoinRound);        CGContextStrokePath(currentContext);

先设置画笔的颜色,这个比喻我还是比较喜欢的,整个程序看起来就像是画笔在屏幕上画;在设置画笔的宽度,完后确定画笔起点,确定画笔终点,按道理说花完一条线你还想画第二条线,你又该确定画笔的起点,但是机制是你画完第一条线画笔就到了终点,如果你想以此终点作为第二条线的起点,那你就直接确定下一个终点就可以了;确定好每段的终点后就可以把这些点用直线连起来了,代码中有个CGContextSetLineJoin(currentContext, kCGLineJoinRound);这个是来确定两个线相交点的平滑处理的,有三中效果。

上述的方法就比较简陋,作者引入了path路径的概念,有了路径概念;书上解释的比较麻烦,我自己理解了一下就是要你确定各个路径点,不管是直线还是多边形也好,把这些点和路径信息一起存起来最终一起绘制,这才是强大之处,来段代码:

    /* Create the path */    CGMutablePathRef path = CGPathCreateMutable();    /* How big is our screen? We want the X to cover the whole screen */    CGRect screenBounds = [[UIScreen mainScreen] bounds];    /* Start from top-left */    CGPathMoveToPoint(path, NULL,screenBounds.origin.x, screenBounds.origin.y);    /* Draw a line from top-left to bottom-right of the screen */    CGPathAddLineToPoint(path,NULL, screenBounds.size.width, screenBounds.size.height);    /* Start another line from top-right */    CGPathMoveToPoint(path,NULL, screenBounds.size.width, screenBounds.origin.y);    /* Draw a line from top-right to bottom-left */    CGPathAddLineToPoint(path,NULL, screenBounds.origin.x, screenBounds.size.height);            CGRect rectange = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);    CGPathAddRect(path, NULL, rectange);        /* Get the context that the path has to be drawn on */    CGContextRef currentContext = UIGraphicsGetCurrentContext();    /* Add the path to the context so we can draw it later */    CGContextAddPath(currentContext, path);    /* Set the blue color as the stroke color */    [[UIColor blueColor] setStroke];        CGContextSetLineWidth(currentContext,                          5.0f);    [[UIColor colorWithRed:0.20f                     green:0.60f blue:0.80f                     alpha:1.0f] setFill];        /* Draw the path with stroke color */    CGContextDrawPath(currentContext, kCGPathFillStroke);    /* Finally release the path object */    CGPathRelease(path);

带注释的原书用path画线的,我把画矩形的代码抠出来贴上去没有注释,一同将他们存入路径信息里,最终便一起绘制了出来,绘制也有参数,我这次选了kCGPathFillStroke这样就可以在矩形里填充颜色了,线和填充的颜色都可以设置。

今天就这么多吧。

原创粉丝点击