
元素大小= 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)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)