舍入视图的右上角和左角? [英] Round out the top right and left corners of a view?

查看:41
本文介绍了舍入视图的右上角和左角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何屏蔽 UIView 以显示一个形状,该形状代表一个正方形,上面有一个圆的一半.

基本上,这就像只对顶部的两个角应用边框半径以将它们四舍五入并创建一个圆弧.

我正在使用 swift,这让我发疯了.

CornerRadius 不符合我的要求.我觉得我必须使用 CGShapeLayer 但我不确定如何创建弧线.

编辑 3:

考虑到解释我想要什么,提供图片,并解释我不明白我需要做什么,或者我需要如何做对某些人来说是不够的,以下是我尝试过的:

尝试设置 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 类允许您定义由直线和曲线线段组成的路径,并在您的自定义视图中呈现该路径.

  1. 在 UIBezierPath 中生成路径的圆角
  2. 要生成 CAShapeLayer,请将其设置为路径之一
  3. 图层到您想要在一侧仅将圆角 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.

  1. Generate rounded corners of the path in UIBezierPath
  2. To generate a CAShapeLayer, it is set to one of the path
  3. layer to the view that you want to on one side only rounded corners 2 as the mask layer

Example:

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:

这篇关于舍入视图的右上角和左角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆