大佬教程收集整理的这篇文章主要介绍了iphone – 只是两个圆角?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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 – 只是两个圆角?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。