2016-08-11 21:02:26 +03:00
|
|
|
//
|
|
|
|
// ViewController.swift
|
2016-08-15 19:55:11 +03:00
|
|
|
// ChromaColorPicker-Demo
|
2016-08-11 21:02:26 +03:00
|
|
|
//
|
|
|
|
// Created by Cardasis, Jonathan (J.) on 8/11/16.
|
|
|
|
// Copyright © 2016 Jonathan Cardasis. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2017-07-24 23:14:05 +03:00
|
|
|
import ChromaColorPicker
|
2016-08-11 21:02:26 +03:00
|
|
|
|
2019-04-12 04:38:30 +03:00
|
|
|
class ViewController: UIViewController {
|
|
|
|
@IBOutlet weak var colorDisplayView: UIView!
|
|
|
|
|
2019-04-12 05:14:07 +03:00
|
|
|
let colorPicker = ChromaColorPicker()
|
2019-04-17 05:57:38 +03:00
|
|
|
let brightnessSlider = ChromaBrightnessSlider()
|
2019-04-12 04:38:30 +03:00
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2019-04-17 05:57:38 +03:00
|
|
|
setupColorPicker()
|
|
|
|
setupBrightnessSlider()
|
2019-04-19 23:45:47 +03:00
|
|
|
setupColorPickerHandles()
|
2019-04-17 05:57:38 +03:00
|
|
|
}
|
|
|
|
|
2019-11-09 00:17:06 +03:00
|
|
|
override var preferredStatusBarStyle: UIStatusBarStyle {
|
|
|
|
return .lightContent
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
private var homeHandle: ChromaColorHandle! // reference to home handle
|
|
|
|
|
2019-04-17 05:57:38 +03:00
|
|
|
private func setupColorPicker() {
|
2019-04-19 23:45:47 +03:00
|
|
|
colorPicker.delegate = self
|
2019-04-12 04:38:30 +03:00
|
|
|
colorPicker.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(colorPicker)
|
|
|
|
|
2019-04-17 05:57:38 +03:00
|
|
|
let verticalOffset = -defaultColorPickerSize.height / 6
|
|
|
|
|
2019-04-12 04:38:30 +03:00
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
colorPicker.centerXAnchor.constraint(equalTo: view.centerXAnchor),
|
2019-04-17 05:57:38 +03:00
|
|
|
colorPicker.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: verticalOffset),
|
|
|
|
colorPicker.widthAnchor.constraint(equalToConstant: defaultColorPickerSize.width),
|
|
|
|
colorPicker.heightAnchor.constraint(equalToConstant: defaultColorPickerSize.height)
|
2019-04-12 04:38:30 +03:00
|
|
|
])
|
|
|
|
}
|
2016-08-11 22:51:22 +03:00
|
|
|
|
2019-04-17 05:57:38 +03:00
|
|
|
private func setupBrightnessSlider() {
|
|
|
|
brightnessSlider.connect(to: colorPicker)
|
2017-07-25 23:57:14 +03:00
|
|
|
|
2019-04-17 05:57:38 +03:00
|
|
|
// Style
|
2019-05-02 17:29:52 +03:00
|
|
|
brightnessSlider.trackColor = UIColor.blue
|
2019-04-17 05:57:38 +03:00
|
|
|
brightnessSlider.handle.borderWidth = 3.0 // Example of customizing the handle's properties.
|
2017-07-25 23:57:14 +03:00
|
|
|
|
2019-04-17 05:57:38 +03:00
|
|
|
// Layout
|
|
|
|
brightnessSlider.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(brightnessSlider)
|
2016-08-11 22:51:22 +03:00
|
|
|
|
2019-04-17 05:57:38 +03:00
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
brightnessSlider.centerXAnchor.constraint(equalTo: colorPicker.centerXAnchor),
|
|
|
|
brightnessSlider.topAnchor.constraint(equalTo: colorPicker.bottomAnchor, constant: 28),
|
|
|
|
brightnessSlider.widthAnchor.constraint(equalTo: colorPicker.widthAnchor, multiplier: 0.9),
|
|
|
|
brightnessSlider.heightAnchor.constraint(equalTo: brightnessSlider.widthAnchor, multiplier: brightnessSliderWidthHeightRatio)
|
|
|
|
])
|
2016-08-11 21:02:26 +03:00
|
|
|
}
|
2019-04-19 23:45:47 +03:00
|
|
|
|
|
|
|
private func setupColorPickerHandles() {
|
2019-11-09 00:17:06 +03:00
|
|
|
// (Optional) Assign a custom handle size - all handles appear as the same size
|
|
|
|
// colorPicker.handleSize = CGSize(width: 48, height: 60)
|
|
|
|
|
|
|
|
// 1. Add handle and then customize
|
2019-05-02 17:29:52 +03:00
|
|
|
addHomeHandle()
|
|
|
|
|
2019-11-09 00:17:06 +03:00
|
|
|
// 2. Add a handle via a color
|
2019-05-02 17:29:52 +03:00
|
|
|
let peachColor = UIColor(red: 1, green: 203 / 255, blue: 164 / 255, alpha: 1)
|
|
|
|
colorPicker.addHandle(at: peachColor)
|
2019-11-09 00:17:06 +03:00
|
|
|
|
|
|
|
// 3. Create a custom handle and add to picker
|
|
|
|
let customHandle = ChromaColorHandle()
|
|
|
|
customHandle.color = UIColor.purple
|
|
|
|
colorPicker.addHandle(customHandle)
|
2019-05-02 17:29:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private func addHomeHandle() {
|
2019-11-09 00:17:06 +03:00
|
|
|
homeHandle = colorPicker.addHandle(at: .blue)
|
2019-04-21 00:52:46 +03:00
|
|
|
|
|
|
|
// Setup custom handle view with insets
|
|
|
|
let customImageView = UIImageView(image: #imageLiteral(resourceName: "home").withRenderingMode(.alwaysTemplate))
|
|
|
|
customImageView.contentMode = .scaleAspectFit
|
|
|
|
customImageView.tintColor = .white
|
|
|
|
homeHandle.accessoryView = customImageView
|
2019-05-02 17:29:52 +03:00
|
|
|
homeHandle.accessoryViewEdgeInsets = UIEdgeInsets(top: 2, left: 4, bottom: 4, right: 4)
|
2019-04-19 23:45:47 +03:00
|
|
|
}
|
2016-08-11 21:02:26 +03:00
|
|
|
}
|
|
|
|
|
2019-04-19 23:45:47 +03:00
|
|
|
extension ViewController: ChromaColorPickerDelegate {
|
2019-05-02 17:29:52 +03:00
|
|
|
func colorPickerHandleDidChange(_ colorPicker: ChromaColorPicker, handle: ChromaColorHandle, to color: UIColor) {
|
2019-04-19 23:45:47 +03:00
|
|
|
colorDisplayView.backgroundColor = color
|
2019-11-09 00:17:06 +03:00
|
|
|
|
|
|
|
// Here I can detect when the color is too bright to show a white icon
|
|
|
|
// on the handle and change its tintColor.
|
|
|
|
if handle === homeHandle, let imageView = homeHandle.accessoryView as? UIImageView {
|
|
|
|
let colorIsBright = color.isLight
|
|
|
|
|
|
|
|
UIView.animate(withDuration: 0.2, animations: {
|
|
|
|
imageView.tintColor = colorIsBright ? .black : .white
|
|
|
|
}, completion: nil)
|
|
|
|
}
|
2019-04-19 23:45:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-17 05:57:38 +03:00
|
|
|
private let defaultColorPickerSize = CGSize(width: 320, height: 320)
|
|
|
|
private let brightnessSliderWidthHeightRatio: CGFloat = 0.1
|