Skip to content

Instantly share code, notes, and snippets.

@DonMag
Last active August 10, 2018 07:37
Show Gist options
  • Save DonMag/8fbf454c4e20c2fa6861520b93b065e0 to your computer and use it in GitHub Desktop.
Save DonMag/8fbf454c4e20c2fa6861520b93b065e0 to your computer and use it in GitHub Desktop.
class ShapeShadowViewController: UIViewController {
var starView: UIView!
var roundedView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
starView = UIView()
starView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(starView)
starView.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
starView.heightAnchor.constraint(equalTo: starView.widthAnchor, multiplier: 1.0).isActive = true
starView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100.0).isActive = true
starView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0.0).isActive = true
starView.backgroundColor = .clear
roundedView = UIView()
roundedView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(roundedView)
roundedView.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
roundedView.heightAnchor.constraint(equalTo: roundedView.widthAnchor, multiplier: 1.0).isActive = true
roundedView.topAnchor.constraint(equalTo: starView.bottomAnchor, constant: 60.0).isActive = true
roundedView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0.0).isActive = true
roundedView.backgroundColor = .clear
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let starMaskLayer = CAShapeLayer()
let xCenter = starView.bounds.midX
let yCenter = starView.bounds.midY
let xThird = starView.bounds.width * 0.33
let yThird = starView.bounds.height * 0.33
let xMax = starView.bounds.maxX
let yMax = starView.bounds.maxY
let starPath = UIBezierPath()
starPath.move(to: CGPoint(x: xCenter, y: 0.0))
starPath.addLine(to: CGPoint(x: xThird * 2.0, y: yThird))
starPath.addLine(to: CGPoint(x: xMax, y: yCenter))
starPath.addLine(to: CGPoint(x: xThird * 2.0, y: yThird * 2.0))
starPath.addLine(to: CGPoint(x: xCenter, y: yMax))
starPath.addLine(to: CGPoint(x: xThird, y: yThird * 2.0))
starPath.addLine(to: CGPoint(x: 0.0, y: yCenter))
starPath.addLine(to: CGPoint(x: xThird, y: yThird))
starPath.close()
starMaskLayer.frame = starView.bounds
starMaskLayer.fillColor = UIColor.red.cgColor
starMaskLayer.path = starPath.cgPath
starView.layer.addSublayer(starMaskLayer)
starView.layer.shadowOpacity = 0.9
starView.layer.shadowOffset = CGSize(width: -0.1, height: 4)
roundedView.layer.shadowRadius = 3.0
starView.layer.shadowColor = UIColor.lightGray.cgColor
let roundedShapeLayer = CAShapeLayer()
let roundedMaskPath = UIBezierPath(roundedRect: roundedView.bounds,
byRoundingCorners: [.bottomLeft, .bottomRight],
cornerRadii: CGSize(width: 14, height: 14))
roundedShapeLayer.frame = roundedView.bounds
roundedShapeLayer.fillColor = UIColor.cyan.cgColor
roundedShapeLayer.path = roundedMaskPath.cgPath
roundedView.layer.addSublayer(roundedShapeLayer)
roundedView.layer.shadowOpacity = 0.9
roundedView.layer.shadowOffset = CGSize(width: -0.1, height: 4)
roundedView.layer.shadowRadius = 3.0
roundedView.layer.shadowColor = UIColor.lightGray.cgColor
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment