Heim > Fragen und Antworten > Hauptteil
我写了一个方法,用来给控件指定的角切圆角,方法如下
- (void)maskToCorner:(UIView *)view RoundingCorners:(UIRectCorner)corner cornerRedius:(CGSize)size {
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:corner
cornerRadii:size];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = view.bounds;
maskLayer.path = maskPath.CGPath;
view.layer.mask = maskLayer;
}
调用方法如下:
[_tools maskToCorner:self.cardView RoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRedius:CGSizeMake(10.0f,10.0f)];
结果只能使左上角变成圆角,右上角并没有被切,无论CGSizeMake(width,height)中的width变为多大,都只是左上角起了作用,现在不知道产生这个问题的原因是什么,还请指教。谢谢。
怪我咯2017-04-17 17:37:19
override func drawRect(rect: CGRect) {
//切左上右上圆角
let maskPath = UIBezierPath(roundedRect:self.bounds, byRoundingCorners: [.TopLeft,.TopRight], cornerRadii: CGSizeMake(5.0, 5.0))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = maskPath.CGPath
self.layer.mask = maskLayer
//添加border
let borderLayer = CAShapeLayer()
borderLayer.frame = self.bounds
borderLayer.path = maskPath.CGPath
borderLayer.lineWidth = 0.5
borderLayer.strokeColor = UIColor.lightGrayColor().CGColor
borderLayer.fillColor = UIColor.clearColor().CGColor
let layers:NSArray = self.layer.sublayers! as NSArray
if ((layers.lastObject!.isKindOfClass(CAShapeLayer))) {
layers.lastObject?.removeFromSuperlayer()
}
self.layer.addSublayer(borderLayer)
}