UIView の左上と右上のコーナーのみに cornerRadius を設定するにはどうすればいいですか? 質問する

UIView の左上と右上のコーナーのみに cornerRadius を設定するにはどうすればいいですか? 質問する

cornerRadiusの左上隅と右上隅だけを設定する方法はありますかUIView?

以下のことを試してみましたが、結局ビューが表示されなくなりました。

UIView *view = [[UIView alloc] initWithFrame:frame];

CALayer *layer = [CALayer layer];
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRoundedRect:frame byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(3.0, 3.0)];
layer.shadowPath = shadowPath.CGPath;
view.layer.mask = layer;

ベストアンサー1

なぜあなたの解決策が機能しなかったのかはわかりませんが、次のコードは私にとっては機能しています。ベジェ マスクを作成し、それをビューに適用します。以下の私のコードでは、_backgroundView半径 3 ピクセルでの下隅を丸めていました。selfはカスタムですUITableViewCell

UIBezierPath *maskPath = [UIBezierPath
    bezierPathWithRoundedRect:self.backgroundImageView.bounds
    byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
    cornerRadii:CGSizeMake(20, 20)
];

CAShapeLayer *maskLayer = [CAShapeLayer layer];

maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;

self.backgroundImageView.layer.mask = maskLayer;

いくつかの改良を加えた Swift 2.0 バージョン:

let path = UIBezierPath(roundedRect:viewToRound.bounds, byRoundingCorners:[.TopRight, .BottomLeft], cornerRadii: CGSizeMake(20, 20))
let maskLayer = CAShapeLayer()

maskLayer.path = path.CGPath
viewToRound.layer.mask = maskLayer

Swift 3.0 バージョン:

let path = UIBezierPath(roundedRect:viewToRound.bounds,
                        byRoundingCorners:[.topRight, .bottomLeft],
                        cornerRadii: CGSize(width: 20, height:  20))

let maskLayer = CAShapeLayer()

maskLayer.path = path.cgPath
viewToRound.layer.mask = maskLayer

Swift 拡張機能ここ

おすすめ記事