如何仅裁剪视图的左侧和右侧?
How to clip a view only the left and right sides?
我只想剪裁视图中子视图的左侧和右侧。
因此,子视图的内容位于超级视图的顶部和底部。
我该怎么做?
设置父视图主层的mask
属性:
// The mask has the same width as parentView but much taller. It is not visible
// by itself so so can set `dy` to any value. Negative values for `dy` means
// making it grow in height
let maskRect = parentView.bounds.insetBy(dx: 0, dy: -1000)
let maskPath = UIBezierPath(rect: maskRect)
let maskLayer = CAShapeLayer()
maskLayer.path = maskPath.cgPath
// The color is not important here, its opacity matters more. Opaque or
// partially-opaque pixels in the mask will allow subviews to show through.
// Fully transparent pixels will hide it. Kinda backwards if you ask me
maskLayer.fillColor = UIColor.black.cgColor
parentView.layer.mask = maskLayer
这里有几张插图。 parentView
是黑色线框。
没有面具:
带遮罩(只剪裁左右两边):
我只想剪裁视图中子视图的左侧和右侧。 因此,子视图的内容位于超级视图的顶部和底部。 我该怎么做?
设置父视图主层的mask
属性:
// The mask has the same width as parentView but much taller. It is not visible
// by itself so so can set `dy` to any value. Negative values for `dy` means
// making it grow in height
let maskRect = parentView.bounds.insetBy(dx: 0, dy: -1000)
let maskPath = UIBezierPath(rect: maskRect)
let maskLayer = CAShapeLayer()
maskLayer.path = maskPath.cgPath
// The color is not important here, its opacity matters more. Opaque or
// partially-opaque pixels in the mask will allow subviews to show through.
// Fully transparent pixels will hide it. Kinda backwards if you ask me
maskLayer.fillColor = UIColor.black.cgColor
parentView.layer.mask = maskLayer
这里有几张插图。 parentView
是黑色线框。
没有面具:
带遮罩(只剪裁左右两边):