├── .gitignore ├── LICENSE ├── Package.swift ├── README.md ├── Screenshots └── example.png └── Sources └── AlphaSlider └── AlphaSlider.swift /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | .swiftpm 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 David Boyes 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.3 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "AlphaSlider", 8 | products: [ 9 | .library( 10 | name: "AlphaSlider", 11 | targets: ["AlphaSlider"]), 12 | ], 13 | dependencies: [ 14 | ], 15 | targets: [ 16 | .target( 17 | name: "AlphaSlider", 18 | dependencies: []), 19 | ] 20 | ) 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AlphaSlider 2 | ![](https://github.com/DavidBoyes/AlphaSlider/blob/master/Screenshots/example.png?raw=true) 3 | 4 | A visual improvement of UISlider to be used to specify the alpha value of a color. 5 | 6 | # Features 7 | * Requires no images 8 | * Can be used with Interface Builder 9 | * Color property can be dynamically changed via code 10 | 11 | # Usage 12 | Works just like a UISlider with the addition of a color property. This sets the backgrounds gradient color, the default is black. Set either directly in code or use the color property Interface Builder. 13 | 14 | # Installation 15 | Either use the AlphaSlider.swift file directly by downloading and dragging it in to your Xcode project or use via Swift Package Manager. 16 | ```sh 17 | dependencies: [ 18 | .package(url: "https://github.com/DavidBoyes/AlphaSlider", from: "1.0.0") 19 | ] 20 | ``` 21 | 22 | # Author 23 | [David Boyes](https://twitter.com/davidboyes) 24 | 25 | # License 26 | AlphaSlider is released under the MIT license. See LICENSE for details. 27 | -------------------------------------------------------------------------------- /Screenshots/example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DavidBoyes/AlphaSlider/11feb0b212ad89d75afa959ce5a2d348671f16dd/Screenshots/example.png -------------------------------------------------------------------------------- /Sources/AlphaSlider/AlphaSlider.swift: -------------------------------------------------------------------------------- 1 | #if canImport(UIKit) 2 | import UIKit 3 | import CoreImage 4 | 5 | @available(iOS 10.0, *) 6 | @IBDesignable open class AlphaSlider: UISlider { 7 | 8 | @IBInspectable var color : UIColor? { 9 | didSet { 10 | self.setNeedsDisplay() 11 | } 12 | } 13 | 14 | private var background : UIImage? 15 | 16 | init(frame: CGRect, color: UIColor = .black) { 17 | super.init(frame: frame) 18 | setup(color) 19 | } 20 | 21 | override init(frame: CGRect) { 22 | super.init(frame: frame) 23 | setup(.black) 24 | } 25 | 26 | required public init?(coder: NSCoder) { 27 | super.init(coder: coder) 28 | setup(.black) 29 | } 30 | 31 | private func setup(_ color: UIColor) { 32 | self.color = color 33 | self.maximumTrackTintColor = .clear 34 | self.minimumTrackTintColor = .clear 35 | self.tintColor = .clear 36 | 37 | self.maximumValue = 1.0 38 | self.minimumValue = 0.0 39 | 40 | let context = CIContext() 41 | let scale = UIScreen.main.scale 42 | let filter = CIFilter(name: "CICheckerboardGenerator", parameters: ["inputColor0" : CIColor.white, "inputColor1" : CIColor.gray, "inputCenter" : CIVector(x: 0, y: 0), "inputWidth" : 5*scale]) 43 | 44 | guard let outputImage = filter?.outputImage else { return } 45 | guard let image = context.createCGImage(outputImage, from: CGRect(x: bounds.minX * scale, y: bounds.minY * scale, width: (bounds.width) * scale, height: bounds.height * scale)) else { return } 46 | 47 | background = UIImage(cgImage: image, scale: scale, orientation: .up) 48 | setNeedsDisplay() 49 | } 50 | 51 | open override func tintColorDidChange() { 52 | self.maximumTrackTintColor = .clear 53 | self.minimumTrackTintColor = .clear 54 | self.tintColor = .clear 55 | } 56 | 57 | open override func draw(_ rect: CGRect) { 58 | let path = UIBezierPath(roundedRect: CGRect(x: 5, y: 0, width: rect.width-10, height: rect.height), cornerRadius: rect.height/2) 59 | 60 | let context = UIGraphicsGetCurrentContext()! 61 | let colors = [color!.withAlphaComponent(0).cgColor, color!.cgColor] 62 | let colorSpace = CGColorSpaceCreateDeviceRGB() 63 | 64 | context.addPath(path.cgPath) 65 | context.clip() 66 | 67 | if let bg = background { 68 | bg.draw(at: .zero) 69 | } 70 | 71 | let colorLocations: [CGFloat] = [0.0, 1.0] 72 | let gradient = CGGradient(colorsSpace: colorSpace, 73 | colors: colors as CFArray, 74 | locations: colorLocations)! 75 | 76 | let startPoint = CGPoint(x: 5, y: 0) 77 | let endPoint = CGPoint(x: bounds.width-20, y: 0) 78 | context.drawLinearGradient(gradient, 79 | start: startPoint, 80 | end: endPoint, 81 | options: [.drawsAfterEndLocation]) 82 | 83 | context.setStrokeColor(UIColor.init(white: 0, alpha: 0.2).cgColor) 84 | path.lineWidth = 1 85 | path.stroke() 86 | 87 | } 88 | } 89 | #endif 90 | --------------------------------------------------------------------------------