iOS Draw Rectagle具有弯曲的末端 [英] iOS Draw Rectagle with curved ends

查看:161
本文介绍了iOS Draw Rectagle具有弯曲的末端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一个长矩形(可能大小为200x20)。所有面都有直边。在我的iOS应用程序中,这对我来说很容易绘制:

Imagine a long rectangle (maybe with size 200x20). All sides have straight edges. In my iOS app, this is easy for me to draw:

CGContextFillRect(context, CGRectMake(xLoc, yLoc, 200, 20));

如果我想要较短的一端(在矩形的左侧和右侧),该怎么办?略微弯曲,而不是直边。代码看起来会是什么样的?

Now what if I wanted the shorter ends (on the left and right side of the rectangle) to be slightly curved, rather than straight edges. What would the code look like to do this?

(注意我对CGContext绘图相对缺乏经验)

(Note that I am relatively inexperienced with CGContext drawing)

推荐答案

这样的事情(未经测试,因此要注意漏洞!):

Something like this (untested, so beware of bugs!):

- (void)drawRect:(CGRect)rect {
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
 CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);

 CGRect rrect = CGRectMake(CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetWidth(rect)-30, CGRectGetHeight(rect)-30);
 CGFloat radius = 0.5f;

 CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
 CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);

 CGContextMoveToPoint(context, minx, midy);
 CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
 CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
 CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
 CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
 CGContextClosePath(context);
 CGContextDrawPath(context, kCGPathFillStroke);
}

这篇关于iOS Draw Rectagle具有弯曲的末端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆