IOS开发UI篇--使用CAShapeLayer实现复杂的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