在swift中创建一个只有两个圆角的矩形? [英] Create a rectangle with just two rounded corners in swift?
本文介绍了在swift中创建一个只有两个圆角的矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在swift中创建一个只有两个圆角的矩形(Objective C代码也可以)。
I need to create a rectangle that have just two rounded corners in swift (Objective C code also ok).
目前我的代码正在创建两个带有矩形的矩形
At the moment my code is creating two rectangles with
CGPathCreateWithRoundedRect(CGRectMake(0, 0, 30, 60), 5, 5, nil);
和
CGPathCreateWithRoundedRect(CGRectMake(0, 0, 30, 60), 0, 0, nil);
并合并它们(有两个直角和两个圆角)但我不满意代码,我很确定应该有更好的方法来实现它。
and merging them (to have two right angle corners and two rounded ones) but I am not happy with the code and I am pretty sure there should be much better ways to do it.
我是iOS和图形开发的新手,并且很快。
I am new to iOS and graphical development and swift.
推荐答案
在 Swift 2.3 中你可以通过
let maskPath = UIBezierPath(roundedRect: anyView.bounds,
byRoundingCorners: [.BottomLeft, .BottomRight],
cornerRadii: CGSize(width: 10.0, height: 10.0))
let shape = CAShapeLayer()
shape.path = maskPath.CGPath
view.layer.mask = shape
在 Objective-C 中,您可以使用 UIBezierPath
类方法
In Objective-C you could use the UIBezierPath
class method
bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:
示例实现 -
// set the corner radius to the specified corners of the passed container
- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
view.layer.mask = shape;
}
并将上述方法称为 -
and call the above method as-
[self setMaskTo:anyView byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];
这篇关于在swift中创建一个只有两个圆角的矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文