只有两个圆angular?

在我的iPad应用程序中,当用户单击button时,我希望第二个视图出现在主视图中。 新视图将比第一个视图小,并在显示时将背景变暗。 我希望新视图的顶部两个angular落看起来四舍五入,但是使用cornerRadius将所有angular色都设置为圆angular。 我怎样才能使两个angular落四舍五入?

你必须在drawRect中做到这一点。 我实际上修改了经典的addRoundedRectToPath:以便它需要一个位图并且四舍五入您请求的angular落:

static void addRoundedRectToPath(CGContextRef context, CGRect rect, float radius, UIImageRoundedCorner cornerMask) { CGContextMoveToPoint(context, rect.origin.x, rect.origin.y + radius); CGContextAddLineToPoint(context, rect.origin.x, 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.x, rect.origin.y + rect.size.height); CGContextAddLineToPoint(context, rect.origin.x + radius, 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, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1); } else { CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height); CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height - radius); } CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + radius); if (cornerMask & UIImageRoundedCornerBottomRight) { CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, radius, 0.0f, -M_PI / 2, 1); } else { CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y); CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius, rect.origin.y); } CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y); if (cornerMask & UIImageRoundedCornerBottomLeft) { CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + radius, radius, -M_PI / 2, M_PI, 1); } else { CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y); CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + radius); } CGContextClosePath(context); } 

这需要一个位掩码(我称之为UIImageRoundedCorner,因为我是这样做的图像,但你可以调用它),然后build立一个基于你想四舍五入的angular落的path。 然后,将该path应用于drawRect中的视图:

 CGContextBeginPath(context); addRoundedRectToPath(context, rect, radius, yourMask); CGContextClosePath(context); CGContextClip(context); 

正如我所说的,我为UIImages做了这个工作,所以我的代码并没有在drawRect中使用,但是它很容易适应。 你基本上只是build立一条path,然后剪辑它的上下文。

编辑:解释位掩码部分,它只是一个枚举:

 typedef enum { UIImageRoundedCornerTopLeft = 1, UIImageRoundedCornerTopRight = 1 << 1, UIImageRoundedCornerBottomRight = 1 << 2, UIImageRoundedCornerBottomLeft = 1 << 3 } UIImageRoundedCorner; 

通过这种方式,你可以把东西放在一起形成一个掩码来标识angular点,因为枚举的每个成员在位掩码中表示不同的二的幂。

编辑:我发现一个更简单的方法来做到这一点使用UIBezierPathbezierPathWithRoundedRect:byRoundingCorners:cornerRadii: 只需在上下文中使用bezierpath对象的CGPath

  // Create the path (with only the top-left corner rounded) UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerTopLeft| UIRectCornerTopRight cornerRadii:CGSizeMake(10.0, 10.0)]; // Create the shape layer and set its path CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = imageView.bounds; maskLayer.path = maskPath.CGPath; // Set the newly created shape layer as the mask for the image view's layer imageView.layer.mask = maskLayer; 

这是其他使用顶部圆angular。 绕过UIView的两个angular落

TomekKuźma所做的精彩的工作。 这是您的要求的新的TKRoundedView类。
您的要求只能通过此参数来实现。

 TKRoundedView *view = [[TKRoundedView alloc] initWithFrame:frame]; view.roundedCorners = TKRoundedCornerTopLeft | TKRoundedCornerTopRight; 

但它也提供了以下额外function。

 TKRoundedView *view = [[TKRoundedView alloc] initWithFrame:frame]; view.roundedCorners = TKRoundedCornerTopLeft view.borderColor = [UIColor greenColor]; view.fillColor = [UIColor whiteColor]; view.drawnBordersSides = TKDrawnBorderSidesLeft | TKDrawnBorderSidesTop; view.borderWidth = 5.0f; view.cornerRadius = 15.0f; 

请检查示例项目的以下链接
https://github.com/mapedd/TKRoundedView