尝试设置 layer.cornerRadius 属性,同时将视图底部推出屏幕以隐藏底部角落,以免出现截断,这种类型的遮罩太圆,没有提供正确的电弧结果.我尝试使用 UIBezierPath 设置顶部的两个角以使用视图 width/2 的 cornerRadii.这也没有产生适当的结果.在尝试将值硬编码到 cornerRadii 时,我注意到无论值如何,我似乎都无法获得我想要的结果.还有哪些选择?还是我只是错误地使用了 BezierPath?编辑 4:这是我的原始代码.override func viewWillAppear(animated: Bool) {super.viewWillAppear(动画)let bezierPath = UIBezierPath(roundedRect: navBarView.bounds, byRoundingCorners: [.TopLeft, .TopRight],cornerRadii: CGSizeMake(navBarView.bounds.width/2, navBarView.bounds.width/2)).CGPath;让 shapeLayer = CAShapeLayer();shapeLayer.bounds = navBarView.boundsshapeLayer.path = bezierPathnavBarView.layer.mask = shapeLayerupdateUI()//<-- 标签文本等}另外,如前所述,曾尝试做 navBarView.layer.cornerRadius = x 解决方案 试试这个:let path = UIBezierPath(roundedRect:self.view.bounds, byRoundingCorners:[.TopLeft, .TopRight],cornerRadii: CGSizeMake(20, 20))让 maskLayer = CAShapeLayer()maskLayer.frame = self.view.bounds;maskLayer.path = path.CGPathself.view.layer.mask = maskLayer;UIBezierPath 类允许您定义由直线和曲线线段组成的路径,并在您的自定义视图中呈现该路径.在 UIBezierPath 中生成路径的圆角要生成 CAShapeLayer,请将其设置为路径之一图层到您想要在一侧仅将圆角 2 作为蒙版图层的视图示例:let DynamicView=UIView(frame: CGRectMake(50, 200, 200, 300))DynamicView.backgroundColor=UIColor.redColor()让路径 = UIBezierPath(roundedRect:DynamicView.bounds, byRoundingCorners:[.TopLeft, .TopRight],cornerRadii: CGSizeMake(20, 20))让 maskLayer = CAShapeLayer()maskLayer.frame = DynamicView.bounds;maskLayer.path = path.CGPathDynamicView.layer.mask = maskLayer;self.view.addSubview(DynamicView)结果:I'm trying to figure out how to mask a UIView to display a shape that represents a Square with half of a circle sitting ontop of it.Basically, it would be like applying a border radius to only the top two corners to round them out and create an arc.I'm using swift, and this is driving me crazy.EDIT: CornerRadius does not do what I want. I feel like I'll have to use a CGShapeLayer but I'm not sure how to create the arc.EDIT2:EDIT3:Considering explaining what I want, providing pictures, and explaining that I didn't understand what I needed to do, or how I needed to do it isn't enough for some people, here's what I've tried:Attempted setting the layer.cornerRadius property, while pushing the bottom of the view out past the screen to hide the bottom corners from appearing cut off, this type of masking was too circular and did not provide the proper arc results.I've attempted using UIBezierPath setting the top two corners to use a cornerRadii of the views width / 2. This also did not yield proper results. Upon attempting to hardcode values into the cornerRadii, I noticed that regardless of the value, I could not seem to obtain the results that I had wanted.What other options are there? Or am I just using the BezierPath incorrectly?EDIT 4:Here is my original code.override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) let bezierPath = UIBezierPath(roundedRect: navBarView.bounds, byRoundingCorners: [.TopLeft, .TopRight], cornerRadii: CGSizeMake(navBarView.bounds.width / 2, navBarView.bounds.width / 2)).CGPath; let shapeLayer = CAShapeLayer(); shapeLayer.bounds = navBarView.bounds shapeLayer.path = bezierPath navBarView.layer.mask = shapeLayer updateUI() // <-- Label text, etc.}Also, as stated before, had attempted doing navBarView.layer.cornerRadius = x 解决方案 Try this:let path = UIBezierPath(roundedRect:self.view.bounds, byRoundingCorners:[.TopLeft, .TopRight], cornerRadii: CGSizeMake(20, 20)) let maskLayer = CAShapeLayer() maskLayer.frame = self.view.bounds; maskLayer.path = path.CGPath self.view.layer.mask = maskLayer;The UIBezierPath class lets you define a path consisting of straight and curved line segments and render that path in your custom views.Generate rounded corners of the path in UIBezierPathTo generate a CAShapeLayer, it is set to one of the pathlayer to the view that you want to on one side only rounded corners 2 as the mask layerExample:let DynamicView=UIView(frame: CGRectMake(50, 200, 200, 300)) DynamicView.backgroundColor=UIColor.redColor() let path = UIBezierPath(roundedRect:DynamicView.bounds, byRoundingCorners:[.TopLeft, .TopRight], cornerRadii: CGSizeMake(20, 20)) let maskLayer = CAShapeLayer() maskLayer.frame = DynamicView.bounds; maskLayer.path = path.CGPath DynamicView.layer.mask = maskLayer; self.view.addSubview(DynamicView)Result: 这篇关于舍入视图的右上角和左角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 13:27