Siv*_*iva 32 iphone xcode objective-c ipad ios
有没有办法只为UIView的左下角,右下角和左上角设置cornerRadius?
我尝试了下面这个,但最终让视图消失了.下面的代码有什么问题吗?
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(20.0, 20.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.path = maskPath.CGPath;
view.layer.mask = maskLayer;
Run Code Online (Sandbox Code Playgroud)
Jus*_*Boo 134
你可以这样做:
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.viewOutlet.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
self.viewOutlet.layer.mask = maskLayer;
Run Code Online (Sandbox Code Playgroud)
更新:
如果您需要边框,只需创建另一个CAShapeLayer
并将其添加到视图的图层作为子图层.像这样(将此代码放在上面的代码下面):
CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init];
borderLayer.frame = self.view.bounds;
borderLayer.path = maskPath.CGPath;
borderLayer.lineWidth = 4.0f;
borderLayer.strokeColor = [UIColor blackColor].CGColor;
borderLayer.fillColor = [UIColor clearColor].CGColor;
[self.viewOutlet.layer addSublayer:borderLayer];
Run Code Online (Sandbox Code Playgroud)
在swift 3.0中像这样:
let maskPath = UIBezierPath.init(roundedRect: self.viewOutlet.bounds, byRoundingCorners:[.topLeft, .bottomLeft], cornerRadii: CGSize.init(width: 10.0, height: 10.0))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.viewOutlet.bounds
maskLayer.path = maskPath.cgPath
self.viewOutlet.layer.mask = maskLayer
Run Code Online (Sandbox Code Playgroud)
Mus*_*ari 16
在xcode 8和swift 3中测试过
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
}
}
Run Code Online (Sandbox Code Playgroud)
并使用这样的
YourView.roundCorners([.topLeft, .bottomLeft], radius: 10)
Run Code Online (Sandbox Code Playgroud)
使用自定义UIView
并实现以下代码
#import "CustomUIView.h"
@implementation CustomView
- (void)drawRect:(CGRect)rect
{
// Drawing code
CAShapeLayer * maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: self.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii: (CGSize){20.0, 20.0}].CGPath;
self.layer.mask = maskLayer;
}
@end
Run Code Online (Sandbox Code Playgroud)
输出:
看看UIBezierPath.h
选项:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
Run Code Online (Sandbox Code Playgroud)
设置视图的角半径 Swift
层的创建和屏蔽应该在drawRect()
继承自的可选块内部运行UIView
override func drawRect(rect: CGRect) {
super.drawRect(rect)
let maskPath = UIBezierPath(roundedRect: self.view.bounds, byRoundingCorners: [.TopLeft, .BottomLeft, .BottomRight], cornerRadii: CGSizeMake(10, 10))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.view.bounds
maskLayer.path = maskPath.CGPath
self.view.layer.mask = maskLayer
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
42875 次 |
最近记录: |