1. 程式人生 > >畫圓形View(將view中間部分摳出圓形透明)

畫圓形View(將view中間部分摳出圓形透明)

http://blog.csdn.net/yixiangboy/article/details/50485250?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io

(將view中間部分摳出圓形透明)通過在上面覆蓋一層view,扣掉中間部分圓形為透明,

func cutTheMiddleSection(view:UIView) {

let w:CGFloat = view.frame.size.width

let h:CGFloat = view.frame.size.height

let path =UIBezierPath(rect:

CGRectMake(0, 0, w, h))

let pathCircle =UIBezierPath(arcCenter:CGPointMake(w / 2, h / 2), radius: w / 2, startAngle: 0, endAngle:CGFloat(2*M_PI), clockwise:false)

        path.appendPath(pathCircle)

let shapeLayer =CAShapeLayer()

        shapeLayer.path = path.CGPath

        view.layer.mask = shapeLayer

    }

// 繪製圓圖

UIGraphicsBeginImageContextWithOptions(ImageView.bounds.size,false,UIScreen.mainScreen().scale)

UIBezierPath(roundedRect: ImageView.bounds, cornerRadius: cornerRadius).addClip()

UIImage(data: imageData!)?.drawInRect(ImageView.bounds)

                        ImageView.image =UIGraphicsGetImageFromCurrentImageContext

()

UIGraphicsEndImageContext()

// 設定圓角度

imageView.layer.cornerRadius = imageView.frame.width / 2

imageView.clipsToBounds = true

卡得厲害的話,你只能做一張跟 imageView 一樣大小的黑白圖了,中間是白的,圓角矩形,剩下的是黑的,將這個圖做為layer.mask

用了 mask 就不要 cornerRadius