【发布时间】:2011-06-18 05:29:44
【问题描述】:
在我的 iPad 应用程序中,我希望在用户单击按钮时在主视图中显示第二个视图。新视图将比第一个视图小,并在显示时使背景变暗。我希望新视图的前两个角看起来是圆形的,但是使用cornerRadius 会将它们全部设置为圆形。我怎样才能使两个角变成圆角?
【问题讨论】:
标签: iphone objective-c ipad rounded-corners
在我的 iPad 应用程序中,我希望在用户单击按钮时在主视图中显示第二个视图。新视图将比第一个视图小,并在显示时使背景变暗。我希望新视图的前两个角看起来是圆形的,但是使用cornerRadius 会将它们全部设置为圆形。我怎样才能使两个角变成圆角?
【问题讨论】:
标签: iphone objective-c ipad rounded-corners
您必须在 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.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,因为我是为图像执行此操作,但您可以随便调用它),然后根据您想要圆角的角构建路径。然后将该路径应用到 drawRect 中的视图:
CGContextBeginPath(context);
addRoundedRectToPath(context, rect, radius, yourMask);
CGContextClosePath(context);
CGContextClip(context);
正如我所说,我是为 UIImages 执行此操作的,因此我的代码并未完全设置为用于 drawRect:,但它应该很容易适应它。你基本上只是建立一个路径,然后将上下文剪辑到它。
编辑:为了解释位掩码部分,它只是一个枚举:
typedef enum {
UIImageRoundedCornerTopLeft = 1,
UIImageRoundedCornerTopRight = 1 << 1,
UIImageRoundedCornerBottomRight = 1 << 2,
UIImageRoundedCornerBottomLeft = 1 << 3
} UIImageRoundedCorner;
通过这种方式,您可以将事物组合在一起以形成标识角的位掩码,因为枚举的每个成员都代表位掩码中 2 的不同幂。
很久以后编辑:
我发现了一种使用UIBezierPath 的bezierPathWithRoundedRect:byRoundingCorners:cornerRadii: 的更简单方法。只需在您的上下文中使用贝塞尔路径对象的CGPath。
【讨论】:
在目标 C 中
// 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;
这是另一种使用顶部圆角的方法。 Round two corners in UIView
在斯威夫特!!!
myView.clipsToBounds = true
myView.layer.cornerRadius = 10
myView.layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]
【讨论】:
Tomek Kuźma 的出色工作。这是满足您要求的新 TKRoundedView 类。
仅此参数即可满足您的要求。
TKRoundedView *view = [[TKRoundedView alloc] initWithFrame:frame];
view.roundedCorners = TKRoundedCornerTopLeft | TKRoundedCornerTopRight;
但它还提供以下额外功能。
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
【讨论】: