iOS中的随机颜色

iOS中的随机颜色,第1张

概述我希望每次加载Navbar时都使用不同的颜色.我在viewDidApear中放置了以下代码: CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white 我希望每次加载Navbar时都使用不同的颜色.我在vIEwDIDApear中放置了以下代码:

CGfloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0CGfloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0,away from whiteCGfloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0,away from blackUIcolor *color = [UIcolor colorWithHue:hue saturation:saturation brightness:brightness Alpha:1];self.navigationbar.barTintcolor = color;

问题是颜色范围太宽.

我希望它只选择你在这张照片中看到的颜色:

是否可以使用此代码?如果不是我将如何创建一个类似的,从我定义的几个选择随机颜色.

谢谢你的帮助.

解决方法 这是使用您的确切颜色的复制/粘贴解决方案.

// Declare somewhere in your codetypedef struct _color {    CGfloat red,green,blue;} color;static color _colors[12] = {    {237,230,4},// Yellow just to the left of center    {158,209,16},// Next color clockwise (green)    {80,181,23},{23,144,103},{71,110,175},{159,73,172},{204,66,162},{255,59,167},88,0},129,{254,172,204,0}};- (UIcolor *)randomcolor {    color randomcolor = _colors[arc4random_uniform(12)];    return [UIcolor colorWithRed:(randomcolor.red / 255.0f) green:(randomcolor.green / 255.0f) blue:(randomcolor.blue / 255.0f) Alpha:1.0f];}

注意:您应该使用arc4random_uniform()而不是arc4random()来避免模偏差(尽管在这种情况下不是那么重要).

总结

以上是内存溢出为你收集整理的iOS中的随机颜色全部内容,希望文章能够帮你解决iOS中的随机颜色所遇到的程序开发问题。

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

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

原文地址: https://outofmemory.cn/web/1060684.html

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

发表评论

登录后才能评论

评论列表(0条)

保存