Skip to content

Instantly share code, notes, and snippets.

@ppamorim
Last active May 18, 2020 22:05
Show Gist options
  • Save ppamorim/feb3318ac30e20a75d9c62e8abdc2efa to your computer and use it in GitHub Desktop.
Save ppamorim/feb3318ac30e20a75d9c62e8abdc2efa to your computer and use it in GitHub Desktop.
//: 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
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)
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)
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)
// guard let context = UIGraphicsGetCurrentContext() else {return}
// context.addEllipse(in: CGRect(x: 10.0, y: 10.0, 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: 0.0, y: rect.height))
path.addLine(to: CGPoint(x: 0.0, y: radius))
path.addCurve(
to: CGPoint(x: radius, y: 0.0),
controlPoint1: CGPoint(x: 0.0, y: radius * MAGIC_NUMBER),
controlPoint2: CGPoint(x: radius * MAGIC_NUMBER, y: 0.0))
let firstPointStart = CGPoint(x: rect.width/2 - ballRadius/2 - radius, y: 0.0)
path.addLine(to: firstPointStart)
let firstPointEnd: CGPoint = CGPoint(x: firstPointStart.x + radius, y: firstPointStart.y + radius)
path.addCurve(
to: firstPointEnd,
controlPoint1: CGPoint(x: firstPointStart.x + radius * MAGIC_NUMBER, y: firstPointStart.y),
controlPoint2: CGPoint(x: firstPointEnd.x, y: firstPointEnd.y - radius * MAGIC_NUMBER))
let middleLeft: CGPoint = CGPoint(x: firstPointEnd.x, y: ballRadius/2)
path.addLine(to: middleLeft)
let secondPointEnd: CGPoint = CGPoint(x: middleLeft.x + ballRadius/2, y: middleLeft.y + ballRadius/2)
let offset = ballRadius * MAGIC_NUMBER
print("offset \(offset)")
path.addCurve(
to: secondPointEnd,
controlPoint1: CGPoint(x: firstPointEnd.x, y: secondPointEnd.y - offset),
controlPoint2: CGPoint(x: firstPointEnd.x, y: secondPointEnd.y))
let thirdPointStart: CGPoint = CGPoint(x: secondPointEnd.x + ballRadius/2, y: secondPointEnd.y - ballRadius/2)
path.addCurve(
to: thirdPointStart,
controlPoint1: CGPoint(x: thirdPointStart.x, y: secondPointEnd.y),
controlPoint2: CGPoint(x: thirdPointStart.x, y: secondPointEnd.y - (ballRadius * MAGIC_NUMBER)))
let middleRight: CGPoint = CGPoint(x: thirdPointStart.x, y: firstPointEnd.y)
path.addLine(to: middleRight)
let fourthPoint: CGPoint = CGPoint(x: middleRight.x + radius, y: 0)
path.addCurve(
to: fourthPoint,
controlPoint1: CGPoint(x: middleRight.x, y: radius * MAGIC_NUMBER),
controlPoint2: CGPoint(x: middleRight.x + radius * MAGIC_NUMBER, y: 0))
let endTop: CGPoint = CGPoint(x: rect.width - radius, y: 0)
path.addLine(to: endTop)
path.addCurve(
to: CGPoint(x: rect.width, y: radius),
controlPoint1: CGPoint(x: endTop.x + radius * MAGIC_NUMBER, y: 0),
controlPoint2: CGPoint(x: rect.width, y: radius * MAGIC_NUMBER))
path.addLine(to: CGPoint(x: rect.width, y: rect.height))
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: 300, height: 300)
PlaygroundPage.current.liveView = viewController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment