Merge pull request #6 from Marcocanc/corner-radius

Add configurable cornerRadius
This commit is contained in:
Tosin Afolabi 2019-03-14 13:43:55 -07:00 committed by GitHub
commit 6438b952cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 21 deletions

View File

@ -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)
@ -799,31 +798,17 @@ private extension PanModalPresentationController {
because we render the dragIndicator outside of view bounds
*/
func addRoundedCorners(to view: UIView) {
let radius = presentable?.cornerRadius ?? 0
let path = UIBezierPath(roundedRect: view.bounds,
byRoundingCorners: [.topLeft, .topRight],
cornerRadii: CGSize(width: radius, height: radius))
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)
// 2. Draw around the drag indicator view, if displayed
// 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

View File

@ -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)
}
@ -77,6 +77,10 @@ public extension PanModalPresentable where Self: UIViewController {
return isPanModalPresented
}
var cornerRadius: CGFloat {
return 8
}
var showDragIndicator: Bool {
return shouldRoundTopCorners
}

View File

@ -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.

View File

@ -59,6 +59,7 @@ class PanModalTests: XCTestCase {
XCTAssertEqual(vc.shouldRoundTopCorners, false)
XCTAssertEqual(vc.showDragIndicator, false)
XCTAssertEqual(vc.shouldRoundTopCorners, false)
XCTAssertEqual(vc.cornerRadius, 8)
}
func testPresentableYValues() {