
static voID addRoundedRecttopath(CGContextRef context,CGRect rect,float radius,UIImageRoundedCorner cornerMask){ CGContextMovetoPoint(context,rect.origin.x,rect.origin.y + radius); CGContextAddlinetoPoint(context,rect.origin.y + rect.size.height - radius); if (cornerMask & UIImageRoundedCornertopleft) { CGContextAddArc(context,rect.origin.x + radius,rect.origin.y + rect.size.height - radius,radius,M_PI,M_PI / 2,1); } else { CGContextAddlinetoPoint(context,rect.origin.y + rect.size.height); CGContextAddlinetoPoint(context,rect.origin.y + rect.size.height); } CGContextAddlinetoPoint(context,rect.origin.x + rect.size.wIDth - radius,rect.origin.y + rect.size.height); if (cornerMask & UIImageRoundedCornertopRight) { CGContextAddArc(context,0.0f,rect.origin.x + rect.size.wIDth,rect.origin.y + rect.size.height - radius); } CGContextAddlinetoPoint(context,rect.origin.y + radius); if (cornerMask & UIImageRoundedCornerBottomright) { CGContextAddArc(context,rect.origin.y + radius,-M_PI / 2,rect.origin.y); CGContextAddlinetoPoint(context,rect.origin.y); } CGContextAddlinetoPoint(context,rect.origin.y); if (cornerMask & UIImageRoundedCornerBottomleft) { CGContextAddArc(context,rect.origin.y + radius); } CGContextClosePath(context);} 这需要一个bitmask(我称之为UIImageRoundedCorner,因为我正在做这个图像,但你可以调用它),然后建立一个路径,基于你想要圆角的角落。然后将该路径应用于drawRect中的视图:
CGContextBeginPath(context);addRoundedRecttopath(context,rect,yourMask);CGContextClosePath(context);CGContextClip(context);
正如我所说,我正在为UIImages这样做,所以我的代码并不完全用于drawRect:,但它应该很容易适应。你基本上只是建立一条路径,然后剪切上下文。
编辑:要解释位掩码部分,它只是一个枚举:
typedef enum { UIImageRoundedCornertopleft = 1,UIImageRoundedCornertopRight = 1 << 1,UIImageRoundedCornerBottomright = 1 << 2,UIImageRoundedCornerBottomleft = 1 << 3} UIImageRoundedCorner; 以这种方式,您可以将事物组合在一起,形成一个标识角色的位掩码,因为枚举中的每个成员表示位掩码中的两个不同的权力。
稍后编辑:
我发现使用UIBezierPath的bezIErPathWithRoundedRect:byRoundingCorners:cornerRadii:发现了一个更简单的方法。只需在您的上下文中使用bezIEr路径对象的CGPath。
以上是内存溢出为你收集整理的iphone – 只是两个圆角?全部内容,希望文章能够帮你解决iphone – 只是两个圆角?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)