-
-
Save ppamorim/9390708c455a950a65621715ce7b6c82 to your computer and use it in GitHub Desktop.
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
//: A UIKit based Playground for presenting user interface | |
import UIKit | |
import PlaygroundSupport | |
let BALL: CGFloat = 100 | |
class MyViewController : UIViewController { | |
override func loadView() { | |
let view = UIView() | |
view.backgroundColor = UIColor.black | |
let label = UILabel() | |
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20) | |
label.text = "Hello World!" | |
label.textColor = .black | |
view.addSubview(label) | |
let bottom = BottomBarView() | |
bottom.translatesAutoresizingMaskIntoConstraints = false | |
// bottom.radius = 30 | |
bottom.frame = CGRect.zero | |
bottom.backgroundColor = UIColor.red | |
let button = UIView() | |
button.translatesAutoresizingMaskIntoConstraints = false | |
button.backgroundColor = UIColor.yellow | |
button.layer.cornerRadius = 90/2.0 | |
view.addConstraint(NSLayoutConstraint(item: button, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 90)) | |
view.addConstraint(NSLayoutConstraint(item: button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 90)) | |
view.addConstraint(NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: bottom, attribute: .top, multiplier: 1, constant: 5.0)) | |
view.addConstraint(NSLayoutConstraint(item: button, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0)) | |
view.addSubview(button) | |
view.addConstraint(NSLayoutConstraint(item: bottom, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0)) | |
view.addConstraint(NSLayoutConstraint(item: bottom, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0)) | |
view.addConstraint(NSLayoutConstraint(item: bottom, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)) | |
view.addConstraint(NSLayoutConstraint(item: bottom, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 131)) | |
view.addSubview(bottom) | |
self.view = view | |
} | |
} | |
final class BottomBarView: UIView { | |
//private let MAGIC_NUMBER: CGFloat = 0.552284749831 | |
private let MAGIC_NUMBER: CGFloat = 0.5 | |
var radius: CGFloat = 30 | |
var ballRadius: CGFloat = BALL | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
roundCorners() | |
} | |
var controlPoints: [CGPoint] = [] | |
override func draw(_ rect: CGRect) { | |
super.draw(rect) | |
controlPoints.forEach { controlPoint in | |
if let context = UIGraphicsGetCurrentContext() { | |
context.addEllipse(in: CGRect(x: controlPoint.x, y: controlPoint.y, width: 10.0, height: 10.0)) | |
context.setFillColor(UIColor.white.cgColor) | |
context.fillPath() | |
} | |
} | |
} | |
func roundCorners() { | |
if layer.mask == nil && radius != 0 { | |
let rect = self.bounds | |
let path = UIBezierPath() | |
path.move(to: CGPoint(x: rect.minX, y: rect.maxY)) | |
path.addArc(withCenter: CGPoint(x: rect.minX + radius, y: rect.minY + radius), radius: radius, startAngle: .pi, endAngle: .pi * 3 / 2, clockwise: true) | |
path.addArc(withCenter: CGPoint(x: rect.maxX/2 - ballRadius/2 - radius, y: rect.minY + radius), radius: radius, startAngle: .pi * 3 / 2, endAngle: 2 * .pi, clockwise: true) | |
path.addArc(withCenter: CGPoint(x: rect.maxX/2, y: ballRadius/2), radius: ballRadius/2, startAngle: .pi, endAngle: .pi * 2, clockwise: false) | |
path.addArc(withCenter: CGPoint(x: rect.maxX/2 + radius + ballRadius/2, y: rect.minY + radius), radius: radius, startAngle: .pi, endAngle: .pi * 3 / 2, clockwise: true) | |
path.addArc(withCenter: CGPoint(x: rect.maxX - radius, y: rect.minY + radius), radius: radius, startAngle: .pi * 3 / 2, endAngle: 2 * .pi, clockwise: true) | |
path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY)) | |
path.close() | |
let maskLayer = CAShapeLayer() | |
maskLayer.path = path.cgPath | |
layer.mask = maskLayer | |
} | |
} | |
} | |
// Present the view controller in the Live View window | |
let viewController = MyViewController() | |
viewController.preferredContentSize = CGSize(width: 500, height: 500) | |
PlaygroundPage.current.liveView = viewController |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment