Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some tweaks to highlighting #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions Classes/Popover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ open class Popover: UIView {
open var dismissOnBlackOverlayTap: Bool = true
open var showBlackOverlay: Bool = true
open var highlightFromView: Bool = false
open var highlightViewBounds: CGRect? = nil
open var highlightCornerRadius: CGFloat = 0
open var springDamping: CGFloat = 0.7
open var initialSpringVelocity: CGFloat = 3
Expand Down Expand Up @@ -139,14 +140,14 @@ open class Popover: UIView {
self.show(contentView, point: point, inView: inView)
}

open func show(_ contentView: UIView, point: CGPoint) {
open func show(_ contentView: UIView, point: CGPoint, fromView: UIView? = nil) {
guard let rootView = UIApplication.shared.keyWindow else {
return
}
self.show(contentView, point: point, inView: rootView)
self.show(contentView, point: point, inView: rootView, fromView: fromView)
}

open func show(_ contentView: UIView, point: CGPoint, inView: UIView) {
open func show(_ contentView: UIView, point: CGPoint, inView: UIView, fromView: UIView? = nil) {
if self.dismissOnBlackOverlayTap || self.showBlackOverlay {
self.blackOverlay.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.blackOverlay.frame = inView.bounds
Expand All @@ -170,7 +171,11 @@ open class Popover: UIView {
self.blackOverlay.addTarget(self, action: #selector(Popover.dismiss), for: .touchUpInside)
}
}


if self.highlightFromView, let fromView = fromView {
self.createHighlightLayer(fromView: fromView, inView: inView)
}

self.containerView = inView
self.contentView = contentView
self.contentView.backgroundColor = UIColor.clear
Expand Down Expand Up @@ -415,8 +420,28 @@ private extension Popover {

func createHighlightLayer(fromView: UIView, inView: UIView) {
let path = UIBezierPath(rect: inView.bounds)
let highlightRect = inView.convert(fromView.frame, from: fromView.superview)
let highlightPath = UIBezierPath(roundedRect: highlightRect, cornerRadius: self.highlightCornerRadius)
var highlightRect = inView.convert(fromView.frame, from: fromView.superview)

var cornerRadii = UIRectCorner.allCorners

if let highlightViewBounds = highlightViewBounds {
let clippedHighlightRect = highlightRect.intersection(highlightViewBounds)
if highlightRect != clippedHighlightRect {
if clippedHighlightRect.origin.y > highlightRect.origin.y {
cornerRadii.remove(UIRectCorner.topLeft)
cornerRadii.remove(UIRectCorner.topRight)
}

if clippedHighlightRect.maxY < highlightRect.maxY {
cornerRadii.remove(UIRectCorner.bottomLeft)
cornerRadii.remove(UIRectCorner.bottomRight)
}
}

highlightRect = clippedHighlightRect
}

let highlightPath = UIBezierPath(roundedRect: highlightRect, byRoundingCorners: cornerRadii, cornerRadii: CGSize(width: self.highlightCornerRadius, height: self.highlightCornerRadius))
path.append(highlightPath)
path.usesEvenOddFillRule = true

Expand Down