Cocoa touch 如何绘制视图的单个圆角。

Cocoa touch 如何绘制视图的单个圆角。,cocoa-touch,uiview,Cocoa Touch,Uiview,嗨!我想在我的ui视图上画一个圆角只有一个,其他的不能更改。从iOS 3.2开始,您可以使用UIBezierPaths的功能创建开箱即用的圆角矩形(其中只有您指定的角是圆角)。然后,您可以将其用作CAShapeLayer的路径,并将其用作视图层的遮罩: // Create the path (with only the top-left corner rounded) UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect

嗨!我想在我的
ui视图
上画一个圆角只有一个,其他的不能更改。

从iOS 3.2开始,您可以使用
UIBezierPath
s的功能创建开箱即用的圆角矩形(其中只有您指定的角是圆角)。然后,您可以将其用作
CAShapeLayer
的路径,并将其用作视图层的遮罩:

// Create the path (with only the top-left corner rounded)
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
                                               byRoundingCorners:UIRectCornerTopLeft
                                                     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;
就这样-不要在核心图形中手动定义形状,也不要在Photoshop中创建遮罩图像。该层甚至不需要失效。应用圆角或更改为新的角点非常简单,只需定义一个新的
UIBezierPath
,并将其
CGPath
用作遮罩层的路径即可。
bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:
方法的
corners
参数是位掩码,因此可以通过将多个角点组合在一起进行取整


注意当与CALayer renderInContext方法结合使用时,层遮罩将不会渲染。如果你需要使用这个方法,试着用以下方法取整:。

我用StuDev的答案做了一个方法:

+ (CAShapeLayer *) roundedCornerOnImage: (UIImageView *)imageView onCorner: (UIRectCorner)rectCorner
{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
                                                   byRoundingCorners:rectCorner
                                                         cornerRadii:CGSizeMake(10.0, 10.0)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = imageView.bounds;
    maskLayer.path = maskPath.CGPath;

    return maskLayer;
}
UITableViewCell中imageview的使用示例:

if (indexPath.row == 0)
    cell.imageView.layer.mask = [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerTopLeft];
else if (indexPath.row == self.arrayPeople.count - 1)
    cell.imageView.layer.mask = [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerBottomLeft];
感谢StuDev提供了一个很好的解决方案

+ (CAShapeLayer *) roundedCornerOnImage: (UIImageView *)imageView onCorner: (UIRectCorner)rectCorner
{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
                                                   byRoundingCorners:rectCorner
                                                         cornerRadii:CGSizeMake(10.0, 10.0)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = imageView.bounds;
    maskLayer.path = maskPath.CGPath;
    imageView.layer.mask=maskLayer
    return maskLayer;
}


if (indexPath.row == 0)
    [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerTopLeft];
else if (indexPath.row == self.arrayPeople.count - 1)
  [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerBottomLeft];

以上更新的答案,您无需返回并管理。可以在该功能中完成。

在进行了一些小的研发之后,我制作了一个定制拐角半径的功能,如下所示:

+(void)setConerRadiusForTopLeft:(BOOL)isForTopLeft ForTopRight:(BOOL)isForTopRight ForBottomLeft:(BOOL)isForBottomLeft  ForBottomRight:(BOOL)isForBottomRight withCornerRadius:(float)cornerRadius forView:(UIView *)view
{

    UIRectCorner corners = (isForTopLeft ? UIRectCornerTopLeft : 0) |
                          (isForTopRight ? UIRectCornerTopRight : 0) |
                          (isForBottomLeft ? UIRectCornerBottomLeft : 0) |
                          (isForBottomRight ? UIRectCornerBottomRight : 0);

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                               byRoundingCorners:corners
                                                     cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
    view.layer.mask = maskLayer;
}

你的代码真的救了我。如果我有机会,我会把这个答案标记为正确的。注意:如果你改变了视图的大小,你将不得不重新创建遮罩。因此,子类UIView并覆盖框架和边界。我是否也可以在该路径周围添加边界以突出显示??我是否可以在此路径周围添加边框以突出显示??