最常用的設(shè)置圓角的方法:
view.layer.cornerRadius = 5
view.layer.masksToBounds = true
但是masksToBounds這個屬性會造成離屏渲染匾旭,這個屬性才是幀數(shù)下降的罪魁禍?zhǔn)住?/p>
如果想簡便的話可以設(shè)置
view.layer.shouldRasterize=true
這個屬性可以為圓角設(shè)置緩存,但是設(shè)置緩存也是需要時間的凉馆,如果有大量的大小不一的圓形視圖出現(xiàn),這樣做依然會出現(xiàn)掉幀。
下面提供兩種思路解決當(dāng)頁面上需要設(shè)置的視圖較多時的解決方案:
一澜共、通過繪制方法向叉,將view的背景圖片重新去掉圓角繪制一遍
1.UIView。注:(該方法只返回UIView類型嗦董,其他類型會出問題)
extension UIView {
func circleView(cornerRadius: CGFloat) -> UIView? {
// 開始圖形上下文
UIGraphicsBeginImageContextWithOptions(self.frame.size, false, 0)
if let context = UIGraphicsGetCurrentContext() {
let rect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)
var path: UIBezierPath
if let color = self.backgroundColor {
CGContextSetLineWidth(context, 1)
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextSetStrokeColorWithColor(context, color.CGColor)
}
if self.frame.size.width == self.frame.size.height {
if cornerRadius == self.frame.size.width/2 {
path = UIBezierPath(arcCenter: self.center, radius: cornerRadius, startAngle: 0, endAngle: 2.0*CGFloat(M_PI), clockwise: true)
}else {
path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
}
}else {
path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
}
self.drawRect(rect)
CGContextAddPath(context, path.CGPath)
CGContextDrawPath(context, .FillStroke)
CGContextClip(context)
// 從上下文上獲取剪裁后的照片
guard let uncompressedImage = UIGraphicsGetImageFromCurrentImageContext() else {
UIGraphicsEndImageContext()
return nil
}
// 關(guān)閉上下文
UIGraphicsEndImageContext()
let view = UIView()
view.backgroundColor = UIColor.clearColor()
view.frame = self.frame
view.addSubview(UIImageView(image: uncompressedImage))
return view
}else {
return nil
}
}
}
2母谎、UIImage(常用方法)
extension UIImage {
func circleImage(cornerRadius: CGFloat, size: CGSize) -> UIImage? {
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
if let context = UIGraphicsGetCurrentContext() {
var path: UIBezierPath
if size.height == size.width {
if cornerRadius == size.width/2 {
path = UIBezierPath(arcCenter: CGPointMake(size.width/2, size.height/2), radius: cornerRadius, startAngle: 0, endAngle: 2.0*CGFloat(M_PI), clockwise: true)
}else {
path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
}
}else {
path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
}
CGContextAddPath(context, path.CGPath)
CGContextClip(context)
self.drawInRect(rect)
// 從上下文上獲取剪裁后的照片
guard let uncompressedImage = UIGraphicsGetImageFromCurrentImageContext() else {
UIGraphicsEndImageContext()
return nil
}
// 關(guān)閉上下文
UIGraphicsEndImageContext()
return uncompressedImage
}else {
return nil
}
}
}
二、通過設(shè)置layer京革,可以設(shè)置切割哪個角奇唤,且設(shè)置邊框
let path = UIBezierPath.init(roundedRect: self.myLabel.bounds, byRoundingCorners: [.topRight , .bottomRight] , cornerRadii: self.myLabel.bounds.size);
let layer = CAShapeLayer.init();
layer.path = path.cgPath;
layer.lineWidth = 5;
layer.lineCap = kCALineCapSquare;
layer.strokeColor = UIColor.red.cgColor;
// 注意直接填充layer的顏色,不需要設(shè)置控件view的backgroundColor
layer.fillColor = UIColor.yellow.cgColor;
myLabel.layer.addSublayer(layer);