如何设置cornerRadius只有左上angular和右上angular的UIView?

有没有办法设置cornerRadius只有左上angular和右上angular的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; 

注意这个事实,如果你有附加的布局约束,你必须刷新这个如下:

  override public func layoutSubviews() { super.layoutSubviews() roundCorners(corners: [.bottomLeft, .bottomRight], radius: UIFlashLabel.cornerRadius) } 

如果你不这样做,它不会显示出来。

我不知道为什么你的解决scheme不起作用,但下面的代码正在为我工​​作。 创build一个贝塞尔面具,并将其应用于您的视图。 在我下面的代码中,我用半径为3像素的_backgroundView四舍五入。 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版本有一些改进:

 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扩展在这里

这里是@JohnnyRockex答案的Swift版本

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

 view.roundCorners([.topLeft, .bottomRight], radius: 10) 

注意

如果您使用自动布局 ,则需要roundCorners UIView子类,并在视图的layoutSubviews调用roundCorners以获得最佳效果。

 class View: UIView { override func layoutSubviews() { super.layoutSubviews() self.roundCorners([.topLeft, .bottomLeft], radius: 10) } } 

Swift代码示例在这里: https : //stackoverflow.com/a/35621736/308315


不直接。 你不得不:

  1. 创build一个CAShapeLayer
  2. 设置它的path为基于view.bounds但只有两个圆angular的view.bounds (可能通过使用+[UIBezierPath bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:]
  3. 设置你的view.layer.maskCAShapeLayer

这里是一个简短的方法实现这样的:

 - (void)viewDidLoad { [super viewDidLoad]; UIButton *openInMaps = [UIButton new]; [openInMaps setFrame:CGRectMake(15, 135, 114, 70)]; openInMaps = (UIButton *)[self roundCornersOnView:openInMaps onTopLeft:NO topRight:NO bottomLeft:YES bottomRight:NO radius:5.0]; } - (UIView *)roundCornersOnView:(UIView *)view onTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius { if (tl || tr || bl || br) { UIRectCorner corner = 0; if (tl) {corner = corner | UIRectCornerTopLeft;} if (tr) {corner = corner | UIRectCornerTopRight;} if (bl) {corner = corner | UIRectCornerBottomLeft;} if (br) {corner = corner | UIRectCornerBottomRight;} UIView *roundedView = view; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = roundedView.bounds; maskLayer.path = maskPath.CGPath; roundedView.layer.mask = maskLayer; return roundedView; } return view; } 

试试这个代码,

 UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:( UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(5.0, 5.0)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.view.bounds; maskLayer.path = maskPath.CGPath; view.layer.mask = maskLayer; 

Emma:.TopRight和.BottomRight不适合你,也许是因为对view.roundCorners的调用是在计算最终的视图边界之前完成的。 请注意,Bezierpath从调用时的视图边界派生。 例如,如果自动布局会缩小视图,那么右侧的圆angular可能会在视图之外。 尝试在viewDidLayoutSubviews中调用它,视图的边界是最终的。

最后…在iOS11中有CACornerMask! 使用CACornerMask可以轻松完成:

 let view = UIView() view.clipsToBounds = true view.layer.cornerRadius = 10 view.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner] 

一种以编程方式做到这一点的方法是在UIView的顶部创build一个具有圆angular的UIView 。 或者你可以隐藏顶部的东西。

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

所有已经给出的答案都是非常好的和有效的(特别是Yunus使用mask属性的想法)。

不过,我需要一些更复杂的东西,因为我的图层经常会改变大小,这意味着我需要每次调用屏蔽逻辑,这有点烦人。

我使用了swift extensions和计算属性来构build一个真正的cornerRadii属性,它在处理图层时负责自动更新蒙版。

这是通过彼得·斯坦伯格伟大的方面库来实现的。

完整的代码在这里:

 extension CALayer { // This will hold the keys for the runtime property associations private struct AssociationKey { static var CornerRect:Int8 = 1 // for the UIRectCorner argument static var CornerRadius:Int8 = 2 // for the radius argument } // new computed property on CALayer // You send the corners you want to round (ex. [.TopLeft, .BottomLeft]) // and the radius at which you want the corners to be round var cornerRadii:(corners: UIRectCorner, radius:CGFloat) { get { let number = objc_getAssociatedObject(self, &AssociationKey.CornerRect) as? NSNumber ?? 0 let radius = objc_getAssociatedObject(self, &AssociationKey.CornerRadius) as? NSNumber ?? 0 return (corners: UIRectCorner(rawValue: number.unsignedLongValue), radius: CGFloat(radius.floatValue)) } set (v) { let radius = v.radius let closure:((Void)->Void) = { let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: v.corners, cornerRadii: CGSize(width: radius, height: radius)) let mask = CAShapeLayer() mask.path = path.CGPath self.mask = mask } let block: @convention(block) Void -> Void = closure let objectBlock = unsafeBitCast(block, AnyObject.self) objc_setAssociatedObject(self, &AssociationKey.CornerRect, NSNumber(unsignedLong: v.corners.rawValue), .OBJC_ASSOCIATION_RETAIN) objc_setAssociatedObject(self, &AssociationKey.CornerRadius, NSNumber(float: Float(v.radius)), .OBJC_ASSOCIATION_RETAIN) do { try aspect_hookSelector("layoutSublayers", withOptions: .PositionAfter, usingBlock: objectBlock) } catch _ { } } } } 

我写了一个简单的博客文章解释这一点。

最简单的方法是制作一个圆angular的面具。

 CALayer *maskLayer = [CALayer layer]; maskLayer.frame = CGRectMake(0,0,maskWidth ,maskHeight); maskLayer.contents = (__bridge id)[[UIImage imageNamed:@"maskImageWithRoundedCorners.png"] CGImage]; aUIView.layer.mask = maskLayer; 

别忘了:

 #import <QuartzCore/QuartzCore.h> 

这是你如何使用C#中的Xamarin设置一个button的每个angular落的angular落半径:

 var maskPath = UIBezierPath.FromRoundedRect(MyButton.Bounds, UIRectCorner.BottomLeft | UIRectCorner.BottomRight, new CGSize(10.0, 10.0)); var maskLayer = new CAShapeLayer { Frame = MyButton.Bounds, Path = maskPath.CGPath }; MyButton.Layer.Mask = maskLayer; 

重用Yunus Nedim Mehel解决scheme的可爱扩展

Swift 2.3

 extension UIView { func roundCornersWithLayerMask(cornerRadii: CGFloat, corners: UIRectCorner) { let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: cornerRadii, height: cornerRadii)) let maskLayer = CAShapeLayer() maskLayer.path = path.CGPath layer.mask = maskLayer } } 

用法

 let view = UIView() view.roundCornersWithLayerMask(10,[.TopLeft,.TopRight]) 

而最简单的方法来做到这一点,你需要2个意见

在这里输入图像描述