Skip to content

Instantly share code, notes, and snippets.

View scottfister's full-sized avatar

Scotty scottfister

  • Apple
  • San Francisco
View GitHub Profile
@scottfister
scottfister / .swift
Created July 27, 2021 19:44
Swift Modulate Function
public extension FloatingPoint {
typealias ModulationRange = (lowerBound: Self, upperBound: Self)
/// Translates the given floating point between two ranges.
/// Limit defaults to true. If limit is set to false, the value returned can be outside of the "to" range.
///
/// Using the Modulate function
/// ----------------------------------------------------------
/// Say you'd like to transform a view's scale from `1.0` to `0.45` depending on some other factor, like scroll amount.
/// The `from` could be `0...120` and the `to` would be`1.0...0.45`, which would mean at `0` content offset, the transform would be `1.0`
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
<integer>1</integer>
</dict>
<key>NSExtensionJavaScriptPreprocessingFile</key>
//...
deck.tapHandler = {
let vc = ShareSelectViewController()
vc.userDecks = self.userDecks
vc.delegate = self
self.pushConfigurationViewController(vc)
}
//...
extension ShareViewController: ShareSelectViewControllerDelegate {
protocol ShareSelectViewControllerDelegate: class {
func selected(deck: Deck)
}
class ShareSelectViewController {
private lazy var tableView: UITableView = {
// ...
tableView.delegate = self
// ...
}()
private var userDecks: [Deck]()
fileprivate var selectedDeck: Deck?
override func viewDidLoad() {
super.viewDidLoad()
for i in 1...3 {
let deck = Deck()
deck.title = "Deck \(i)"
userDecks.append(deck)
}
override func configurationItems() -> [Any]! {
if let deck = SLComposeSheetConfigurationItem() {
deck.title = "Selected Deck"
deck.value = "Deck Title"
deck.tapHandler = {
let vc = ShareSelectViewController()
vc.userDecks = self.userDecks
self.pushConfigurationViewController(vc)
}
return [deck]
private var userDecks = [Deck]()
override func viewDidLoad() {
super.viewDidLoad()
for i in 1...3 {
let deck = Deck()
deck.title = "Deck \(i)"
userDecks.append(deck)
}
// ...
extension ShareSelectViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userDecks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Identifiers.DeckCell, for: indexPath)
cell.textLabel?.text = userDecks[indexPath.row].title
cell.backgroundColor = .clear
return cell
}
lazy var tableView: UITableView = {
let tableView = UITableView(frame: self.view.frame)
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
tableView.dataSource = self
tableView.backgroundColor = .clear
tableView.register(UITableViewCell.self, forCellReuseIdentifier: Identifiers.DeckCell)
return tableView
}()
override func viewDidLoad() {
final class Deck {
var id: String?
var title: String?
}