Skip to content

Commit

Permalink
Add configurable cornerRadius
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcocanc committed Mar 14, 2019
1 parent d9f37de commit d0b0942
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
23 changes: 4 additions & 19 deletions PanModal/Controller/PanModalPresentationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class PanModalPresentationController: UIPresentationController {
Constants
*/
struct Constants {
static let cornerRadius = CGFloat(8.0)
static let indicatorYOffset = CGFloat(8.0)
static let snapMovementSensitivity = CGFloat(0.7)
static let dragIndicatorSize = CGSize(width: 36.0, height: 5.0)
Expand Down Expand Up @@ -797,31 +796,17 @@ private extension PanModalPresentationController {
because we render the dragIndicator outside of view bounds
*/
func addRoundedCorners(to view: UIView) {

let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: Constants.cornerRadius))

// 1. Draw left rounded corner
path.addArc(withCenter: CGPoint(x: path.currentPoint.x + Constants.cornerRadius, y: path.currentPoint.y),
radius: Constants.cornerRadius, startAngle: .pi, endAngle: 3.0 * .pi/2.0, clockwise: true)
let radius = presentable?.cornerRadius ?? 0
let path = UIBezierPath(roundedRect: view.bounds,
byRoundingCorners:[.topRight, .topLeft],
cornerRadii: CGSize(width: radius, height: radius))

// 2. Draw around the drag indicator view, if displayed
if presentable?.showDragIndicator == true {
let indicatorLeftEdgeXPos = view.bounds.width/2.0 - Constants.dragIndicatorSize.width/2.0
drawAroundDragIndicator(currentPath: path, indicatorLeftEdgeXPos: indicatorLeftEdgeXPos)
}

// 3. Draw line to right side of presented view, leaving space to draw rounded corner
path.addLine(to: CGPoint(x: view.bounds.width - Constants.cornerRadius, y: path.currentPoint.y))

// 4. Draw right rounded corner
path.addArc(withCenter: CGPoint(x: path.currentPoint.x, y: path.currentPoint.y + Constants.cornerRadius),
radius: Constants.cornerRadius, startAngle: 3.0 * .pi/2.0, endAngle: 0, clockwise: true)

// 5. Draw around final edges of view
path.addLine(to: CGPoint(x: path.currentPoint.x, y: view.bounds.height))
path.addLine(to: CGPoint(x: 0, y: path.currentPoint.y))

// 6. Set path as a mask to display optional drag indicator view & rounded corners
let mask = CAShapeLayer()
mask.path = path.cgPath
Expand Down
6 changes: 5 additions & 1 deletion PanModal/Presentable/PanModalPresentable+Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public extension PanModalPresentable where Self: UIViewController {
}

var scrollIndicatorInsets: UIEdgeInsets {
let top = shouldRoundTopCorners ? PanModalPresentationController.Constants.cornerRadius : 0
let top = shouldRoundTopCorners ? cornerRadius : 0
return UIEdgeInsets(top: CGFloat(top), left: 0, bottom: bottomLayoutOffset, right: 0)
}

Expand Down Expand Up @@ -77,6 +77,10 @@ public extension PanModalPresentable where Self: UIViewController {
return isPanModalPresented
}

var cornerRadius: CGFloat {
return 8
}

var showDragIndicator: Bool {
return shouldRoundTopCorners
}
Expand Down
7 changes: 7 additions & 0 deletions PanModal/Presentable/PanModalPresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ public protocol PanModalPresentable {
*/
var shouldRoundTopCorners: Bool { get }

/**
The corner radius used when `shouldRoundTopCorners` is enabled.
Default Value is `8.0`
*/
var cornerRadius: CGFloat { get }

/**
A flag to determine if a drag indicator should be shown
above the pan modal container view.
Expand Down

0 comments on commit d0b0942

Please sign in to comment.