简体   繁体   English

向 iOS 应用程序添加颜色选择器

[英]Add a color picker to an iOS app

I'm trying to add a color picker to my iOS application, using Xcode 5. It appears that Xcode offers a color well via the Palettes Panel of Interface Builder , but I can't find the Palettes Panel (nor can I find any documentation of it online beyond that link).我正在尝试使用 Xcode 5 向我的 iOS 应用程序添加一个颜色选择器。 Xcode 似乎通过 Interface Builder 的 Palettes Panel 提供了一种颜色,但我找不到 Palettes Panel(我也找不到任何文档它在该链接之外在线)。

That link also suggests an NSColorWell can be added programatically.该链接还建议可以以编程方式添加NSColorWell I'd prefer to go the Interface Builder route, but if that's not an option sample code would be welcome.我更喜欢走 Interface Builder 路线,但如果这不是一个选项示例代码将受到欢迎。

I had the same question as you.我和你有同样的问题。 It's unfortunate that there is no built in color picker for iOS.不幸的是,iOS 没有内置的颜色选择器。 The other answers here and for similar questions mainly use third party libraries or projects.这里的其他答案和类似问题主要使用第三方库或项目。 I prefer to avoid all the third party stuff whenever possible, so that leaves us with...我宁愿尽可能避免所有第三方的东西,这样我们就......

Make your own color picker制作自己的颜色选择器

There are many ways you could do it, but here is a simple example to show the concept.有很多方法可以做到这一点,但这里有一个简单的例子来展示这个概念。 I set up my story board like this:我这样设置我的故事板:

在此处输入图片说明

It has a UIView (grey here) to show the chosen color, a UIImageView to show the color choices, and a UISlider to choose the color.它有一个UIView (这里是灰色)来显示选择的颜色,一个UIImageView来显示颜色选择,还有一个UISlider来选择颜色。 I used the following image in the UIImageView :我在UIImageView使用了以下图像:

在此处输入图片说明

I made it from the colors of a 12-spoke color wheel using a screen shot and Gimp's color picker tool.我使用屏幕截图和 Gimp 的颜色选择器工具从12 辐色轮的颜色制作它。 Gimp is also useful for getting the color hex codes we will use later. Gimp 对于获取我们稍后将使用的颜色十六进制代码也很有用。

Set the min and max values for the Slider to 0.5 and 13.5.将滑块的最小值和最大值设置为 0.5 和 13.5。 Converting the slider values to integers later will give one number for each of the colors in our image.稍后将滑块值转换为整数将为我们图像中的每种颜色提供一个数字。 Starting at 0.5 rather than 0 makes the slider color change location match the image better.从 0.5 而不是 0 开始会使滑块颜色更改位置更好地匹配图像。

在此处输入图片说明

Hook up the UI elements to the View Controller and use the following code to convert the slider position to colors.将 UI 元素连接到 View Controller 并使用以下代码将滑块位置转换为颜色。

class ViewController: UIViewController {
 
    // RRGGBB hex colors in the same order as the image
    let colorArray = [ 0x000000, 0xfe0000, 0xff7900, 0xffb900, 0xffde00, 0xfcff00, 0xd2ff00, 0x05c000, 0x00c0a7, 0x0600ff, 0x6700bf, 0x9500c0, 0xbf0199, 0xffffff ]
    
    @IBOutlet weak var selectedColorView: UIView!
    @IBOutlet weak var slider: UISlider!
    @IBAction func sliderChanged(sender: AnyObject) {
        selectedColorView.backgroundColor = uiColorFromHex(colorArray[Int(slider.value)])
    }
   
    func uiColorFromHex(rgbValue: Int) -> UIColor {
        
        let red =   CGFloat((rgbValue & 0xFF0000) >> 16) / 0xFF
        let green = CGFloat((rgbValue & 0x00FF00) >> 8) / 0xFF
        let blue =  CGFloat(rgbValue & 0x0000FF) / 0xFF
        let alpha = CGFloat(1.0)
        
        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }
}

Now if you run it, you can choose the color by moving the slider back and forth.现在,如果您运行它,您可以通过来回移动滑块来选择颜色。

在此处输入图片说明

Variations变化

  • Position the slider on top of the image and set the track tints to transparent.将滑块放在图像顶部并将轨道色调设置为透明。 This gives it the feel of a custom UI without having to subclass anything.这给了它自定义 UI 的感觉,而无需子类化任何东西。

在此处输入图片说明

在此处输入图片说明

  • Here is another image with lighter and darker variations of the example project image.这是另一个具有示例项目图像更亮和更暗变化的图像。

在此处输入图片说明

let colorArray = [ 0x000000, 0x262626, 0x4d4d4d, 0x666666, 0x808080, 0x990000, 0xcc0000, 0xfe0000, 0xff5757, 0xffabab, 0xffabab, 0xffa757, 0xff7900, 0xcc6100, 0x994900, 0x996f00, 0xcc9400, 0xffb900, 0xffd157, 0xffe8ab, 0xfff4ab, 0xffe957, 0xffde00, 0xccb200, 0x998500, 0x979900, 0xcacc00, 0xfcff00, 0xfdff57, 0xfeffab, 0xf0ffab, 0xe1ff57, 0xd2ff00, 0xa8cc00, 0x7e9900, 0x038001, 0x04a101, 0x05c001, 0x44bf41, 0x81bf80, 0x81c0b8, 0x41c0af, 0x00c0a7, 0x00a18c, 0x00806f, 0x040099, 0x0500cc, 0x0600ff, 0x5b57ff, 0xadabff, 0xd8abff, 0xb157ff, 0x6700bf, 0x5700a1, 0x450080, 0x630080, 0x7d00a1, 0x9500c0, 0xa341bf, 0xb180bf, 0xbf80b2, 0xbf41a6, 0xbf0199, 0xa10181, 0x800166, 0x999999, 0xb3b3b3, 0xcccccc, 0xe6e6e6, 0xffffff]
  • Use an array of UIColors to avoid having to do the hex conversion.使用一组 UIColors 以避免必须进行十六进制转换。

  • Could use multiple UIViews rather than an image, and then set the colors directly from the array.可以使用多个 UIView 而不是图像,然后直接从数组中设置颜色。

Further study进一步研究

I thought I would throw my color picker into the ring.我想我会把我的颜色选择器扔进戒指。 I use it in my app, You Doodle and I spent a couple weeks making it and testing it in the app.我在我的应用程序You Doodle 中使用它,我花了几个星期制作它并在应用程序中测试它。 It contains a sample project to show you how to get started with it and is open sourced under the MIT license.它包含一个示例项目,向您展示如何开始使用它,并且在 MIT 许可下开源。 It supports any device (iOS 6+), any resolution and portrait and landscape.它支持任何设备(iOS 6+)、任何分辨率以及纵向和横向。 Favorites, recents, color by hue, color wheel and importing textures, as well as deleting and moving favorites to the front is supported.支持收藏夹、最近收藏、按色调着色、色轮和导入纹理,以及删除收藏夹并将其移到前面。

I've tried to combine the good pieces of all the other color pickers and ensure that the MIT license allows a no hassle integration into any project.我试图结合所有其他颜色选择器的优点,并确保 MIT 许可证允许轻松集成到任何项目中。

Github: https://github.com/jjxtra/DRColorPicker Github: https : //github.com/jjxtra/DRColorPicker

Screenshots:截图:

DRColorPicker iPhoneDRColorPicker iPadDRColorPicker iPhoneDRColorPicker iPadDRColorPicker iPhoneDRColorPicker iPad

There is another cool color picker made by kartech . kartech还有一个很酷的颜色选择器。 It has awesome UI for the selection of colors.它有很棒的用户界面来选择颜色。 Moreover you can mark colors as favorite.此外,您可以将颜色标记为最喜欢的。 Link is here .链接在这里

Screenshots:截图:

在此处输入图片说明

如果您的应用的最低部署目标为 iOS 14,则可以使用内置的UIColorPickerViewController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM