如何以编程方式在UIview中制作圆角和侧面? [英] How to make rounded corners and sides in UIview programmatically?

查看:68
本文介绍了如何以编程方式在UIview中制作圆角和侧面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何像在YouTube上一样以编程方式在UIview中制作圆角和侧面:



生成原因为:

 -(void)viewDidLoad {
[super viewDidLoad];

CGFloat宽度= MIN(self.view.bounds.size.width,self.view.bounds.size.width)* 0.7;
CGFloat高度=宽度* 3.0 / 4.0;
CGSize size = CGSizeMake(width,height);
CGFloat角=宽度/ 9.0;

CGPoint中心= CGPointMake(self.view.bounds.size.width / 2.0,self.view.bounds.size.height / 2.0);

CAShapeLayer * layer = [CAShapeLayer层];
layer.path = [self roundedPathAtCenter:center size:size corner:corner] .CGPath;
layer.fillColor = [UIColor redColor] .CGColor;
layer.strokeColor = [UIColor clearColor] .CGColor;
[self.view.layer addSublayer:layer];
}

-(UIBezierPath *)roundedPathAtCenter:(CGPoint)center size:(CGSize)size corner:(CGFloat)corner {
CGFloat width = size.width;
CGFloat高度= size.height;

UIBezierPath * path = [UIBezierPath bezierPath];

//左上角

[路径moveToPoint:CGPointMake(center.x-宽度/ 2.0 +角/ 2.0,center.y-高度/ 2.0 +角/ 2.0 )];

//到顶部中心的路径

[路径addQuadCurveToPoint:CGPointMake(center.x,center.y-高度/ 2.0)controlPoint:CGPointMake(center.x-宽度/ 2.0 +角,center.y-高度/ 2.0)];

//右上

的路径[path addQuadCurveToPoint:CGPointMake(center.x + width / 2.0-corner / 2.0,center.y-height / 2.0 + corner / 2.0)controlPoint:CGPointMake(center.x + width / 2.0-corner,center.y-height / 2.0)];

//右中

的路径[path addQuadCurveToPoint:CGPointMake(center.x + width / 2.0,center.y)controlPoint:CGPointMake(center.x + width / 2.0,center.y-高度/ 2.0 +角)]];

//右下角的路径

[路径addQuadCurveToPoint:CGPointMake(center.x + width / 2.0-corner / 2.0,center.y + height / 2.0-corner / 2.0)controlPoint:CGPointMake(center.x +宽度/2.0,center.y +高度/2.0-角)];

//中心底部的路径

[路径addQuadCurveToPoint:CGPointMake(center.x,center.y + height / 2.0)controlPoint:CGPointMake(center.x + width / 2.0-角,center.y +高度/ 2.0)];

//左下

的路径[path addQuadCurveToPoint:CGPointMake(center.x-width / 2.0 + corner / 2.0,center.y + height / 2.0-corner / 2.0)controlPoint:CGPointMake(center.x-宽度/ 2.0 +角,center.y +高度/ 2.0)];

//左中

的路径[path addQuadCurveToPoint:CGPointMake(center.x-width / 2.0,center.y)controlPoint:CGPointMake(center.x-width / 2.0,center.y +高度/ 2.0-角)]];

//左上角

的路径[path addQuadCurveToPoint:CGPointMake(center.x-width / 2.0 + corner / 2.0,center.y-height / 2.0 + corner / 2.0)controlPoint:CGPointMake(center.x-宽度/ 2.0,center.y-高度/ 2.0 +角)];

[路径closePath];

返回路径;
}

您可以使用这些控制点来获得想要的效果对于。关键是您希望第一个控制点与上一条弧的第二个控制点对齐。



在此示例中,我只是创建一个 CAShapeLayer 并将其添加为子层。另外,您可以使用此 CAShapeLayer 并将其作为 mask 添加到也有其他观点。关键是您只需要创建一个 UIBezierPath 即可绘制所需的轮廓,然后绘制该轮廓,将其添加为子图层或将其用作遮罩。


How to make rounded corners and sides in UIview programmatically like has youtube:

I know how to make rounded corner but have no idea how to make rounded sides. I need to do it programmatically (not just setting image mask previously designed)

解决方案

You can create a UIBezierPath. If you want to just round the corners, you can use bezierPathWithRoundedRect. If you want something more sophisticated, like that YouTube logo, you would use moveToPoint and a series of addCurveToPoint or addQuadCurveToPoint.

For example, this is a close approximation:

That was generated with:

- (void)viewDidLoad {
    [super viewDidLoad];

    CGFloat width  = MIN(self.view.bounds.size.width, self.view.bounds.size.width) * 0.7;
    CGFloat height = width * 3.0 / 4.0;
    CGSize  size   = CGSizeMake(width, height);
    CGFloat corner = width / 9.0;

    CGPoint center = CGPointMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0);

    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = [self roundedPathAtCenter:center size:size corner:corner].CGPath;
    layer.fillColor = [UIColor redColor].CGColor;
    layer.strokeColor = [UIColor clearColor].CGColor;
    [self.view.layer addSublayer:layer];
}

- (UIBezierPath *)roundedPathAtCenter:(CGPoint)center size:(CGSize)size corner:(CGFloat)corner {
    CGFloat width = size.width;
    CGFloat height = size.height;

    UIBezierPath *path = [UIBezierPath bezierPath];

    // upper left corner

    [path moveToPoint: CGPointMake(center.x - width / 2.0 + corner / 2.0, center.y - height / 2.0 + corner / 2.0)];

    // path to top center

    [path addQuadCurveToPoint: CGPointMake(center.x, center.y - height / 2.0) controlPoint: CGPointMake(center.x - width / 2.0 + corner, center.y - height / 2.0)];

    // path to upper right

    [path addQuadCurveToPoint: CGPointMake(center.x + width / 2.0 - corner / 2.0, center.y - height / 2.0 + corner / 2.0) controlPoint: CGPointMake(center.x + width / 2.0 - corner, center.y - height / 2.0)];

    // path to mid right

    [path addQuadCurveToPoint: CGPointMake(center.x + width / 2.0, center.y) controlPoint: CGPointMake(center.x + width / 2.0, center.y - height / 2.0 + corner)];

    // path to lower right

    [path addQuadCurveToPoint: CGPointMake(center.x + width / 2.0 - corner / 2.0, center.y + height / 2.0 - corner / 2.0) controlPoint: CGPointMake(center.x + width / 2.0, center.y + height / 2.0 - corner)];

    // path to center bottom

    [path addQuadCurveToPoint: CGPointMake(center.x, center.y + height / 2.0) controlPoint: CGPointMake(center.x + width / 2.0 - corner, center.y + height / 2.0)];

    // path to lower left

    [path addQuadCurveToPoint: CGPointMake(center.x - width / 2.0 + corner / 2.0, center.y + height / 2.0 - corner / 2.0) controlPoint: CGPointMake(center.x - width / 2.0 + corner, center.y + height / 2.0)];

    // path to mid left

    [path addQuadCurveToPoint: CGPointMake(center.x - width / 2.0, center.y) controlPoint: CGPointMake(center.x - width / 2.0, center.y + height / 2.0 - corner)];

    // path to top left

    [path addQuadCurveToPoint: CGPointMake(center.x - width / 2.0 + corner / 2.0, center.y - height / 2.0 + corner / 2.0) controlPoint: CGPointMake(center.x - width / 2.0, center.y - height / 2.0 + corner)];

    [path closePath];

    return path;
}

You can play around with those control points to get the effect you're looking for. The key is that you want the the first control point to be in line with the second control point of the preceding arc.

In this example, I'm just creating a CAShapeLayer and adding it as a sublayer. Alternatively, you could take this CAShapeLayer and add it as a mask to the layer of some other view, too. The key is that you just need to create a UIBezierPath that draws the outline you want, and either just draw it, add it as a sublayer, or use it as a mask.

这篇关于如何以编程方式在UIview中制作圆角和侧面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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