iphone – 只是两个圆角?

iphone – 只是两个圆角?,第1张

概述参见英文答案 > Rounded UIView using CALayers – only some corners – How?                                    12个答案                                在我的iPad应用程序中,当用户点击按钮时,我想要在主屏幕中显示第二个视图。新视图将小于第一个视图,并在显示时使背景变暗。我 参见英文答案 > Rounded UIView using CALayers – only some corners – How?                                    12个答案                                在我的iPad应用程序中,当用户点击按钮时,我想要在主屏幕中显示第二个视图。新视图将小于第一个视图,并在显示时使背景变暗。我想要新视图的顶部两角出现四舍五入,但是使用cornerRadius集合将它们全部舍入。我怎么能让两个角落四舍五入?解决方法 您必须在drawRect:中执行此 *** 作。我实际上修改了经典的addRoundedRecttopath:它需要一个位图,并绕过你所要求的角落:

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 – 只是两个圆角?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1086728.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存