简单的快速选择器popover(iOS)

简单的快速选择器popover(iOS),第1张

概述有没有一种简单的方法来实现一个颜色选择器popover在迅捷?是否有任何内置的图书馆或UI元素,我可以为此目的利用?我看到一些用Objective-C写的颜色选择器,但是他们几岁,我想知道是否有更新的东西. 这是我所做的一样简单.它只是一个轻量级的UIView,允许您指定元素大小,以防您想要阻止区域(elementSize> 1).它将自身绘制在界面构建器中,以便您可以设置元素大小并查看后果.只需 有没有一种简单的方法来实现一个颜色选择器popover在迅捷?是否有任何内置的图书馆或UI元素,我可以为此目的利用?我看到一些用Objective-C写的颜色选择器,但是他们几岁,我想知道是否有更新的东西.解决方法 这是我所做的一样简单.它只是一个轻量级的UIVIEw,允许您指定元素大小,以防您想要阻止区域(elementSize> 1).它将自身绘制在界面构建器中,以便您可以设置元素大小并查看后果.只需将界面构建器中的一个视图设置为此类,然后将自己设置为委托.有人会在某人点击或拖动它,并在那个位置使用uicolor来告诉你.它将自己画成自己的界限,除了这个课外,没有任何需要,没有形象.

元素大小= 1(默认)

元素大小= 10

internal protocol HSBcolorPickerDelegate : NSObjectProtocol {    func HSBcolorcolorPickertouched(sender:HSBcolorPicker,color:UIcolor,point:CGPoint,state:UIGestureRecognizerState)}@IBDesignableclass HSBcolorPicker : UIVIEw {    weak internal var delegate: HSBcolorPickerDelegate?    let saturationExponenttop:float = 2.0    let saturationExponentBottom:float = 1.3    @IBInspectable var elementSize: CGfloat = 1.0 {        dIDSet {            setNeedsdisplay()        }    }    private func initialize() {        self.clipsToBounds = true        let touchGesture = UILongPressGestureRecognizer(target: self,action: #selector(HSBcolorPicker.touchedcolor(_:)))        touchGesture.minimumPressDuration = 0        touchGesture.allowableMovement = CGfloat.max        self.addGestureRecognizer(touchGesture)    }    overrIDe init(frame: CGRect) {        super.init(frame: frame)        initialize()    }    required init?(coder aDecoder: NSCoder) {        super.init(coder: aDecoder)        initialize()    }    overrIDe func drawRect(rect: CGRect) {        let context = UIGraphicsGetCurrentContext()        for y in (0 as CGfloat).strIDe(to: rect.height,by: elementSize) {            var saturation = y < rect.height / 2.0 ? CGfloat(2 * y) / rect.height : 2.0 * CGfloat(rect.height - y) / rect.height            saturation = CGfloat(powf(float(saturation),y < rect.height / 2.0 ? saturationExponenttop : saturationExponentBottom))            let brightness = y < rect.height / 2.0 ? CGfloat(1.0) : 2.0 * CGfloat(rect.height - y) / rect.height            for x in (0 as CGfloat).strIDe(to: rect.wIDth,by: elementSize) {                let hue = x / rect.wIDth                let color = UIcolor(hue: hue,saturation: saturation,brightness: brightness,Alpha: 1.0)                CGContextSetFillcolorWithcolor(context,color.CGcolor)                CGContextFillRect(context,CGRect(x:x,y:y,wIDth:elementSize,height:elementSize))            }        }    }    func getcolorAtPoint(point:CGPoint) -> UIcolor {        let roundedPoint = CGPoint(x:elementSize * CGfloat(Int(point.x / elementSize)),y:elementSize * CGfloat(Int(point.y / elementSize)))        var saturation = roundedPoint.y < self.bounds.height / 2.0 ? CGfloat(2 * roundedPoint.y) / self.bounds.height            : 2.0 * CGfloat(self.bounds.height - roundedPoint.y) / self.bounds.height        saturation = CGfloat(powf(float(saturation),roundedPoint.y < self.bounds.height / 2.0 ? saturationExponenttop : saturationExponentBottom))        let brightness = roundedPoint.y < self.bounds.height / 2.0 ? CGfloat(1.0) : 2.0 * CGfloat(self.bounds.height - roundedPoint.y) / self.bounds.height        let hue = roundedPoint.x / self.bounds.wIDth        return UIcolor(hue: hue,Alpha: 1.0)    }    func getPointForcolor(color:UIcolor) -> CGPoint {        var hue:CGfloat=0;        var saturation:CGfloat=0;        var brightness:CGfloat=0;        color.getHue(&hue,saturation: &saturation,brightness: &brightness,Alpha: nil);        var yPos:CGfloat = 0        let halfheight = (self.bounds.height / 2)        if (brightness >= 0.99) {            let percentageY = powf(float(saturation),1.0 / saturationExponenttop)            yPos = CGfloat(percentageY) * halfheight        } else {            //use brightness to get Y            yPos = halfheight + halfheight * (1.0 - brightness)        }        let xPos = hue * self.bounds.wIDth        return CGPoint(x: xPos,y: yPos)    }    func touchedcolor(gestureRecognizer: UILongPressGestureRecognizer){        let point = gestureRecognizer.locationInVIEw(self)        let color = getcolorAtPoint(point)        self.delegate?.HSBcolorcolorPickertouched(self,color: color,point: point,state:gestureRecognizer.state)    }}
总结

以上是内存溢出为你收集整理的简单的快速选择器popover(iOS)全部内容,希望文章能够帮你解决简单的快速选择器popover(iOS)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1108850.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存