HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 只是两个圆角?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > 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 – 只是两个圆角?所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。