Last active
February 26, 2021 10:27
-
-
Save morishin/0fe816eae8b54b9eba9c0b1967358054 to your computer and use it in GitHub Desktop.
Circular UIView with drop shadow (using UIBezierPath)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import PlaygroundSupport | |
let container = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 200)) | |
container.backgroundColor = .lightGray | |
let buttonRadius: CGFloat = 50 | |
let buttonSize = CGSize(width: buttonRadius * 2, height: buttonRadius * 2) | |
let buttonPath = UIBezierPath(arcCenter: CGPoint(x: buttonRadius, y: buttonRadius), radius: buttonRadius, startAngle: 0, endAngle: CGFloat.pi * 2, clockwise: true) | |
// only for shadow | |
let buttonBackgroundView = UIView(frame: CGRect(origin: .zero, size: buttonSize)) | |
buttonBackgroundView.backgroundColor = .clear | |
buttonBackgroundView.center = container.center | |
buttonBackgroundView.layer.masksToBounds = false | |
buttonBackgroundView.layer.cornerRadius = buttonRadius | |
buttonBackgroundView.layer.shadowOffset = .zero | |
buttonBackgroundView.layer.shadowRadius = 5 | |
buttonBackgroundView.layer.shadowOpacity = 0.5 | |
buttonBackgroundView.layer.shadowPath = buttonPath.cgPath | |
container.addSubview(buttonBackgroundView) | |
// circle button view | |
let buttonView = UIView(frame: CGRect(origin: .zero, size: buttonSize)) | |
buttonView.backgroundColor = .white | |
buttonView.frame.origin = .zero | |
let buttonShape = CAShapeLayer() | |
buttonShape.path = buttonPath.cgPath | |
buttonView.layer.mask = buttonShape | |
buttonBackgroundView.addSubview(buttonView) | |
PlaygroundPage.current.liveView = container |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! thanks a lot.