I want to cut off the upper and lower portion of a container view in my table view cell using UIBezierPath()
& CAShapeLayer()
. The code is as follows:
func cutView() {
let containerViewHeight: CGFloat = containerView.frame.height
let headerCut: CGFloat = 50
let newHeight = containerViewHeight - headerCut/2
let newHeaderUpperLayer = CAShapeLayer()
let newHeaderLowerLayer = CAShapeLayer()
newHeaderUpperLayer.fillColor = UIColor.black.cgColor
newHeaderLowerLayer.fillColor = UIColor.black.cgColor
containerView.layer.mask = newHeaderUpperLayer
containerView.layer.mask = newHeaderLowerLayer
let getViewFrame = CGRect(x: 0, y: -newHeight, width: containerView.bounds.width, height: containerViewHeight)
let cutDirectionUpper = UIBezierPath()
let cutDirectionLower = UIBezierPath()
cutDirectionUpper.move(to: CGPoint(x: 0, y: 0))
cutDirectionUpper.addLine(to: CGPoint(x: getViewFrame.width, y: headerCut))
cutDirectionUpper.addLine(to: CGPoint(x: getViewFrame.width, y: getViewFrame.height))
cutDirectionUpper.addLine(to: CGPoint(x: 0, y: getViewFrame.height))
cutDirectionLower.move(to: CGPoint(x: 0, y: 0))
cutDirectionLower.addLine(to: CGPoint(x: getViewFrame.width, y: 0))
cutDirectionLower.addLine(to: CGPoint(x: getViewFrame.width, y: getViewFrame.height))
cutDirectionLower.addLine(to: CGPoint(x: 0, y: getViewFrame.height - headerCut))
newHeaderUpperLayer.path = cutDirectionUpper.cgPath
newHeaderLowerLayer.path = cutDirectionLower.cgPath
}
It's working separately but not together. What I am missing here? Any suggestions would be highly appreciated. Thanks in advance.
If I separately run it, It works like this:
But what I want is this:
Using combined UIBezierPath()
:
func cutView() {
let containerViewHeight: CGFloat = containerView.frame.height
let headerCut: CGFloat = 50
let newHeight = containerViewHeight - headerCut/2
let newHeaderLayer = CAShapeLayer()
newHeaderLayer.fillColor = UIColor.black.cgColor
containerView.layer.mask = newHeaderLayer
let getViewFrame = CGRect(x: 0, y: -newHeight, width: containerView.bounds.width, height: containerViewHeight)
let cutDirectionLower = UIBezierPath()
let cutDirectionUpper = UIBezierPath()
cutDirectionUpper.move(to: CGPoint(x: 0, y: 0))
cutDirectionUpper.addLine(to: CGPoint(x: getViewFrame.width, y: headerCut))
cutDirectionUpper.addLine(to: CGPoint(x: getViewFrame.width, y: getViewFrame.height))
cutDirectionUpper.addLine(to: CGPoint(x: 0, y: getViewFrame.height))
cutDirectionLower.move(to: CGPoint(x: 0, y: 0))
cutDirectionLower.addLine(to: CGPoint(x: getViewFrame.width, y: 0))
cutDirectionLower.addLine(to: CGPoint(x: getViewFrame.width, y: getViewFrame.height))
cutDirectionLower.addLine(to: CGPoint(x: 0, y: getViewFrame.height - headerCut))
cutDirectionUpper.append(cutDirectionLower)
newHeaderLayer.path = cutDirectionUpper.cgPath
}
Using combined CGMutablePath()
:
func cutView() {
let containerViewHeight: CGFloat = containerView.frame.height
let headerCut: CGFloat = 50
let newHeight = containerViewHeight - headerCut/2
let newHeaderLayer = CAShapeLayer()
let combinedPath = CGMutablePath()
newHeaderLayer.fillColor = UIColor.black.cgColor
containerView.layer.mask = newHeaderLayer
let getViewFrame = CGRect(x: 0, y: -newHeight, width: containerView.bounds.width, height: containerViewHeight)
let cutDirectionLower = UIBezierPath()
let cutDirectionUpper = UIBezierPath()
cutDirectionUpper.move(to: CGPoint(x: 0, y: 0))
cutDirectionUpper.addLine(to: CGPoint(x: getViewFrame.width, y: headerCut))
cutDirectionUpper.addLine(to: CGPoint(x: getViewFrame.width, y: getViewFrame.height))
cutDirectionUpper.addLine(to: CGPoint(x: 0, y: getViewFrame.height))
cutDirectionLower.move(to: CGPoint(x: 0, y: 0))
cutDirectionLower.addLine(to: CGPoint(x: getViewFrame.width, y: 0))
cutDirectionLower.addLine(to: CGPoint(x: getViewFrame.width, y: getViewFrame.height))
cutDirectionLower.addLine(to: CGPoint(x: 0, y: getViewFrame.height - headerCut))
combinedPath.addPath(cutDirectionUpper.cgPath)
combinedPath.addPath(cutDirectionLower.cgPath)
newHeaderLayer.path = combinedPath
}
Both of them remove the upper and lower cut. What I am missing here?
Output of using combined CGMutablePath()
& combined UIBezierPath()
separately is as follows: