在Swift中如何创建只有两个圆角的矩形?

55

我需要在Swift中创建一个只有两个圆角的矩形(也可以使用Objective C代码)。

目前我的代码正在创建两个矩形

CGPathCreateWithRoundedRect(CGRectMake(0, 0, 30, 60), 5, 5, nil);

并且

CGPathCreateWithRoundedRect(CGRectMake(0, 0, 30, 60), 0, 0, nil);

我想把它们合并在一起(拥有两个直角和两个圆角),但我对代码不满意,我相信应该有更好的方法来完成它。

我是iOS和图形开发以及Swift方面的新手。


1
bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:该方法是 UIBezierPath 类的一个类方法,用于创建一个带有圆角的矩形路径。它需要四个参数:矩形的大小、要圆角的角落、以及每个角落的半径。 - matt
1
通常在Swift中有许多小的更改,例如常量的大小写等。建议向下滚动到最新答案。 - Fattie
请查看我的答案,它会涵盖所有内容:https://dev59.com/t2kw5IYBdhLWcg3wDWTL#68342661 - Vipul Kumar
16个回答

2
extension CACornerMask {

    public static var leftBottom     : CACornerMask { get { return .layerMinXMaxYCorner}}
    public static var rightBottom    : CACornerMask { get { return .layerMaxXMaxYCorner}}
    public static var leftTop        : CACornerMask { get { return .layerMaxXMinYCorner}}
    public static var rightTop       : CACornerMask { get { return .layerMinXMinYCorner}}
}

extension CALayer {

    func roundCorners(_ mask:CACornerMask,corner:CGFloat) {
        self.maskedCorners = mask
        self.cornerRadius = corner
    }
}

self.viewBack.layer.roundCorners([.leftBottom,.rightBottom], corner: 23)

嗨,Mohammad Akbari,欢迎您。请考虑添加解释并正确格式化代码。 - Tiago Martins Peres

1

Swift 5:用于左上角和右上角的圆角。

yourView.layer.cornerRadius = 12
yourView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]

1
总之,您可以创建像这样的漂亮扩展:
extension UIView {

    func roundCorners(_ corners: UIRectCorner, radius: Double) {
        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let shape = CAShapeLayer()
        shape.path = maskPath.cgPath
        layer.mask = shape
    }

}

像这样使用它:
view.roundCorners([.topRight, .bottomRight], radius: 10)

这里是所有角落的值:

  • .topLeft(左上角)
  • .topRight(右上角)
  • .bottomLeft(左下角)
  • .bottomRight(右下角)

1
view.layer.cornerRadius = 10.0
view.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMaxYCorner, .layerMinXMaxYCorner]

Best way to do it!


0

iWasRobbed的答案的Objective-C版本:

UIView+RoundCorners.h

#import <UIKit/UIKit.h>

@interface UIView (RoundCorners)

/**
 Rounds the given set of corners to the specified radius

 - parameter corners: Corners to round
 - parameter radius:  Radius to round to
 */
- (void)roundCorners:(UIRectCorner)corners radius:(CGFloat)radius;

/**
 Rounds the given set of corners to the specified radius with a border

 - parameter corners:     Corners to round
 - parameter radius:      Radius to round to
 - parameter borderColor: The border color
 - parameter borderWidth: The border width
 */
- (void)roundCorners:(UIRectCorner)corners radius:(CGFloat)radius borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth;

/**
 Fully rounds an autolayout view (e.g. one with no known frame) with the given diameter and border

 - parameter diameter:    The view's diameter
 - parameter borderColor: The border color
 - parameter borderWidth: The border width
 */
- (void)fullyRoundWithDiameter:(CGFloat)diameter borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth;

@end

UIView+RoundCorners.m

#import "UIView+RoundCorners.h"

@implementation UIView (RoundCorners)

- (void)roundCorners:(UIRectCorner)corners radius:(CGFloat)radius {
    [self _roundCorners:corners radius:radius];
}

- (void)roundCorners:(UIRectCorner)corners radius:(CGFloat)radius borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth {
    CAShapeLayer *mask = [self _roundCorners:corners radius:radius];
    [self addBorderWithMask:mask borderColor:borderColor borderWidth:borderWidth];
}

- (void)fullyRoundWithDiameter:(CGFloat)diameter borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth {
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = diameter / 2;
    self.layer.borderWidth = borderWidth;
    self.layer.borderColor = borderColor.CGColor;
}

- (CAShapeLayer *)_roundCorners:(UIRectCorner)corners radius:(CGFloat)radius {
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *mask = [CAShapeLayer layer];
    mask.path = path.CGPath;
    self.layer.mask = mask;
    return mask;
}

- (void)addBorderWithMask:(CAShapeLayer *)mask borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth {
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.path = mask.path;
    borderLayer.fillColor = UIColor.clearColor.CGColor;
    borderLayer.strokeColor = borderColor.CGColor;
    borderLayer.lineWidth = borderWidth;
    borderLayer.frame = self.bounds;
    [self.layer addSublayer:borderLayer];
}

@end

0
一个简单的技巧可以如下。像下面图片中的示例一样查看。 红色视图 将具有圆角,而 黄色视图(在红色视图内部)将防止角落变圆。

enter image description here

现在编写以下代码以创建红色视图

        self.myView.layer.cornerRadius = 15

请确保您不要编写任何代码,如 clipsToBounds = true masksToBounds = true

下面的图片是结果

enter image description here

黄色视图的放置位置将决定哪两个角不会被圆角化。希望这很容易实现。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接