Simple swift color picker popover (iOS)

前端 未结 8 1166
被撕碎了的回忆
被撕碎了的回忆 2020-12-12 13:30

Is there is a simple way to implement a color picker popover in swift? Are there any built-in libraries or UI elements that I could leverage for this purpose? I saw some c

8条回答
  •  伪装坚强ぢ
    2020-12-12 13:52

    ColorPickerViewImage

    Based on Christian1313 answer, I added darker colors:

    @IBDesignable final public class SwiftColorView: UIView {
    
    weak var colorSelectedDelegate: ColorDelegate?
    
    @IBInspectable public var numColorsX:Int =  10 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable public var numColorsY:Int = 18 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable public var coloredBorderWidth:Int = 10 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable public var showGridLines:Bool = false {
        didSet {
            setNeedsDisplay()
        }
    }
    
    weak var delegate: SwiftColorPickerDataSource?
    
    public override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        guard let touch = touches.first else { return }
        let location = touch.location(in: self)
    
        colorSelectedDelegate?.setStroke(color: colorAtPoint(point: location))
    }
    
    public override func touchesMoved(_ touches: Set, with event: UIEvent?) {
        guard let touch = touches.first else { return }
        let location = touch.location(in: self)
    
        colorSelectedDelegate?.setStroke(color: colorAtPoint(point: location))
    }
    
    public override func draw(_ rect: CGRect) {
        super.draw(rect)
        let lineColor = UIColor.gray
        let pS = patternSize()
        let w = pS.w
        let h = pS.h
    
        for y in 0.. UIColor
    {
    
        if let ds = delegate {
            return ds.colorForPalletIndex(x: x, y: y, numXStripes: numColorsX, numYStripes: numColorsY)
        } else {
    
            var hue:CGFloat = CGFloat(x) / CGFloat(numColorsX)
            var fillColor = UIColor.white
            if (y==0)
            {
                if (x==(numColorsX-1))
                {
                    hue = 1.0;
                }
                fillColor = UIColor(white: hue, alpha: 1.0);
            }
            else
            {
                if y < numColorsY / 2 {
                    //dark
                    let length = numColorsY / 2
                    let brightness: CGFloat = CGFloat(y) / CGFloat(length)
                    fillColor = UIColor(hue: hue, saturation: 1.0, brightness: brightness, alpha: 1.0)
                } else if y == numColorsY / 2 {
                    // normal
                    fillColor = UIColor(hue: hue, saturation: 1.0, brightness: 1.0, alpha: 1.0)
                } else {
                    // light
                    let length = numColorsY / 2 - 1
                    let offset = y - length - 1
                    let sat:CGFloat = CGFloat(1.0) - CGFloat(offset) / CGFloat(length + 1)
                    print("sat", sat)
                    fillColor = UIColor(hue: hue, saturation: sat, brightness: 1.0, alpha: 1.0)
                }
            }
            return fillColor
        }
    }
    
    func colorAtPoint(point: CGPoint) -> UIColor
    {
        let pS = patternSize()
        let w = pS.w
        let h = pS.h
        let x = (point.x-CGFloat(coloredBorderWidth))/w
        let y = (point.y-CGFloat(coloredBorderWidth))/h
        return colorForRectAt(x: Int(x), y:Int(y))
    }
    
    private func patternSize() -> (w: CGFloat, h:CGFloat)
    {
        let width = self.bounds.width-CGFloat(2*coloredBorderWidth)
        let height = self.bounds.height-CGFloat(2*coloredBorderWidth)
    
        let w = width/CGFloat(numColorsX)
        let h = height/CGFloat(numColorsY)
        return (w,h)
    }
    
    public override func prepareForInterfaceBuilder()
    {
        print("Compiled and run for IB")
    }
    

    }

提交回复
热议问题