mirror of
https://github.com/joncardasis/ChromaColorPicker.git
synced 2024-09-11 06:35:41 +03:00
Added accessory view to handles. Picker now adjusts brightness slider when tap begins and not just moved
This commit is contained in:
parent
9ec96ed57c
commit
b60527a81e
@ -1,5 +1,15 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "20x20",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "20x20",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "29x29",
|
||||
@ -29,6 +39,11 @@
|
||||
"idiom" : "iphone",
|
||||
"size" : "60x60",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ios-marketing",
|
||||
"size" : "1024x1024",
|
||||
"scale" : "1x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
|
6
Example/Assets.xcassets/Contents.json
Normal file
6
Example/Assets.xcassets/Contents.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
21
Example/Assets.xcassets/home.imageset/Contents.json
vendored
Normal file
21
Example/Assets.xcassets/home.imageset/Contents.json
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"filename" : "home.png",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
BIN
Example/Assets.xcassets/home.imageset/home.png
vendored
Normal file
BIN
Example/Assets.xcassets/home.imageset/home.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.0 KiB |
@ -57,7 +57,15 @@ class ViewController: UIViewController {
|
||||
}
|
||||
|
||||
private func setupColorPickerHandles() {
|
||||
colorPicker.addHandle(at: .blue)
|
||||
let homeHandle = colorPicker.addHandle(at: .black)
|
||||
|
||||
// Setup custom handle view with insets
|
||||
let customImageView = UIImageView(image: #imageLiteral(resourceName: "home").withRenderingMode(.alwaysTemplate))
|
||||
customImageView.contentMode = .scaleAspectFit
|
||||
customImageView.tintColor = .white
|
||||
homeHandle.accessoryView = customImageView
|
||||
homeHandle.accessoryViewEdgeInsets = UIEdgeInsets(top: 0, left: 2, bottom: 2, right: 2)
|
||||
|
||||
colorPicker.addHandle(at: .red)
|
||||
}
|
||||
}
|
||||
|
@ -105,9 +105,15 @@ public class ChromaColorPicker: UIControl, ChromaControlStylable {
|
||||
|
||||
for handle in handles {
|
||||
if extendedHitFrame(for: handle).contains(location) {
|
||||
currentHandle = handle
|
||||
colorWheelView.bringSubviewToFront(handle)
|
||||
animateHandleScale(handle, shouldGrow: true)
|
||||
|
||||
if let slider = brightnessSlider {
|
||||
slider.trackColor = handle.color.withBrightness(1)
|
||||
slider.currentValue = slider.value(brightness: handle.color.brightness)
|
||||
}
|
||||
|
||||
currentHandle = handle
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -15,11 +15,29 @@ public class ChromaColorHandle: UIView, ChromaControlStylable {
|
||||
didSet { setNeedsLayout() }
|
||||
}
|
||||
|
||||
/// An image to display above the handle.
|
||||
public var popoverImage: UIImage?
|
||||
/// An image to display in the handle. Updates `accessoryView` to be a UIImageView.
|
||||
public var accessoryImage: UIImage? {
|
||||
didSet {
|
||||
let imageView = UIImageView(image: accessoryImage)
|
||||
imageView.contentMode = .scaleAspectFit
|
||||
accessoryView = imageView
|
||||
}
|
||||
}
|
||||
|
||||
/// A view to display above the handle. Overrides any provided `popoverImage`.
|
||||
public var popoverView: UIView?
|
||||
/// A view to display in the handle. Overrides any previously set `accessoryImage`.
|
||||
public var accessoryView: UIView? {
|
||||
didSet {
|
||||
oldValue?.removeFromSuperview()
|
||||
if let accessoryView = accessoryView {
|
||||
addAccessoryView(accessoryView)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The amount an accessory view's frame should be inset by.
|
||||
public var accessoryViewEdgeInsets: UIEdgeInsets = .zero {
|
||||
didSet { setNeedsLayout() }
|
||||
}
|
||||
|
||||
public var borderWidth: CGFloat = 3.0 {
|
||||
didSet { setNeedsLayout() }
|
||||
@ -54,6 +72,7 @@ public class ChromaColorHandle: UIView, ChromaControlStylable {
|
||||
public override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
layoutHandleShape()
|
||||
layoutAccessoryViewIfNeeded()
|
||||
updateShadowIfNeeded()
|
||||
|
||||
layer.masksToBounds = false
|
||||
@ -98,4 +117,21 @@ public class ChromaColorHandle: UIView, ChromaControlStylable {
|
||||
handleShape.strokeColor = borderColor.cgColor
|
||||
handleShape.lineWidth = borderWidth
|
||||
}
|
||||
|
||||
internal func layoutAccessoryViewIfNeeded() {
|
||||
if let accessoryLayer = accessoryView?.layer {
|
||||
let width = bounds.width - borderWidth * 2
|
||||
let size = CGSize(width: width - (accessoryViewEdgeInsets.left + accessoryViewEdgeInsets.right),
|
||||
height: width - (accessoryViewEdgeInsets.top + accessoryViewEdgeInsets.bottom))
|
||||
accessoryLayer.frame = CGRect(origin: CGPoint(x: (borderWidth / 2) + accessoryViewEdgeInsets.left, y: (borderWidth / 2) + accessoryViewEdgeInsets.top), size: size)
|
||||
|
||||
accessoryLayer.cornerRadius = size.height / 2
|
||||
accessoryLayer.masksToBounds = true
|
||||
}
|
||||
}
|
||||
|
||||
internal func addAccessoryView(_ view: UIView) {
|
||||
let accessoryLayer = view.layer
|
||||
handleShape.addSublayer(accessoryLayer)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user