Skip to content

Commit

Permalink
Added blur as cover.
Browse files Browse the repository at this point in the history
  • Loading branch information
k-o-d-e-n committed Jun 28, 2020
1 parent 7fd3fed commit ce3a5db
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions Example/Podfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
platform :ios, 11.0
use_frameworks!

target 'PopUpButton_Example' do
Expand Down
6 changes: 3 additions & 3 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- PopUpButton (1.0)
- PopUpButton (1.1)

DEPENDENCIES:
- PopUpButton (from `../`)
Expand All @@ -9,8 +9,8 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
PopUpButton: 1769a8faf9471c97f2ab08e5505204dec96bf362
PopUpButton: 514d10349d0907b9ad8d60f8910d49ab4ba7ae59

PODFILE CHECKSUM: 94c6f2f9fa5f0b0f0753affc63203fb233199fc8
PODFILE CHECKSUM: db87d1a7ac8a1795df5fe1bdc80f8349ae082ee7

COCOAPODS: 1.9.1
1 change: 1 addition & 0 deletions Example/PopUpButton/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ViewController: UIViewController {
buttons = (0..<4).map({ i -> PopUpButton in
let button = PopUpButton(items: items)
button.backgroundColor = .black
button.cover = .blur(.dark)
button.layer.cornerRadius = 12
button.currentIndex = Double(i + 1) / 4.0 > 0.5 ? 5 : 15
button.addTarget(self, action: #selector(popUpButtonTouchUpInside), for: .valueChanged)
Expand Down
2 changes: 1 addition & 1 deletion PopUpButton.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'PopUpButton'
s.version = '1.0'
s.version = '1.1'
s.summary = 'A control for selecting an item from a list.'
s.description = <<-DESC
"A control for selecting an item from a list. In other words, single motion `NSPopUpButton` for iOS."
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public final class PopUpButton : UIControl {

public var selectedItemColor: UIColor? { get set }

public var coverBackgroundColor: UIColor? { get set }
public var cover: Cover { get set }

public var anchor: Anchor { get set }

Expand All @@ -25,6 +25,11 @@ public final class PopUpButton : UIControl {
case window
case superview
}

public enum Cover {
case color(UIColor?)
case blur(UIBlurEffect.Style)
}
}

```
Expand Down
25 changes: 21 additions & 4 deletions Sources/PopUpButton/PopUpButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class PopUpButton: UIControl {

public var itemsColor: UIColor?
public var selectedItemColor: UIColor?
public var coverBackgroundColor: UIColor? = #colorLiteral(red: 0.07058823529, green: 0.07058823529, blue: 0.07058823529, alpha: 0.9)
public var cover: Cover = .color(#colorLiteral(red: 0.07058823529, green: 0.07058823529, blue: 0.07058823529, alpha: 0.9))

public var anchor: Anchor = .window
public var items: [Item] = [] {
Expand Down Expand Up @@ -88,9 +88,9 @@ public final class PopUpButton: UIControl {
guard let spaceView = anchor.view(for: self) else { return false }
guard super.beginTracking(touch, with: event) && super.becomeFirstResponder() else { return false }

let cover = UIView(frame: spaceView.bounds)
let (cover, content) = self.cover.build()
cover.frame = spaceView.bounds
spaceView.addSubview(cover)
cover.backgroundColor = coverBackgroundColor

let current = views[currentIndex]
current.backgroundColor = selectedItemColor ?? tintColor
Expand All @@ -100,7 +100,7 @@ public final class PopUpButton: UIControl {
if i != currentIndex {
v.backgroundColor = itemsColor ?? backgroundColor
}
cover.addSubview(v)
content.addSubview(v)
})
self.coverView = cover

Expand Down Expand Up @@ -193,6 +193,23 @@ extension PopUpButton {
}
}

public enum Cover {
case color(UIColor?)
case blur(UIBlurEffect.Style)

func build() -> (view: UIView, contentView: UIView) {
switch self {
case .color(let color):
let view = UIView()
view.backgroundColor = color
return (view, view)
case .blur(let style):
let view = UIVisualEffectView(effect: UIBlurEffect(style: style))
return (view, view.contentView)
}
}
}

private final class ItemView: UIView {
lazy var titleLabel: UILabel = {
let lbl = UILabel()
Expand Down

0 comments on commit ce3a5db

Please sign in to comment.