ios生成随机颜色

写demo,太需要这个随机颜色了,每次都懒得写,从网上找,但是找出来的又都是良莠不齐,还需要再修改。

是在懒得修改,就找了一个合适的,从自己的博客找,减少筛选代码的成本。

OC版本

复制代码
#import <UIKit/UIKit.h>

UIColor *RandomColor(void) {
    CGFloat red = (CGFloat)arc4random_uniform(256) / 255.0;
    CGFloat green = (CGFloat)arc4random_uniform(256) / 255.0;
    CGFloat blue = (CGFloat)arc4random_uniform(256) / 255.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

Swift版本

复制代码
import UIKit
extension UIColor {
    static func randomColor() -> UIColor {
        return UIColor(
            red: .random(in: 0...1),
            green: .random(in: 0...1),
            blue: .random(in: 0...1),
            alpha: 1.0
        )
    }
}
相关推荐
sweet丶16 小时前
理解iOS中Protobuf:一个比JSON更好,但不是替代
ios·性能优化·架构
Mac技巧大咖1 天前
Mac误删文件,废纸篓也清了?Mac时光机器仍可恢复找回
macos·mac误删文件
疯笔码良1 天前
【IOS开发】Instruments 使用指南
ios·swift
程序员果子1 天前
零拷贝:程序性能加速的终极奥秘
linux·运维·nginx·macos·缓存·centos
天庭鸡腿哥1 天前
macOS的功能,在Windows上也能实现
windows·microsoft·macos·visual studio·everything
疯笔码良1 天前
【IOS开发】后台保活方案
ios·swift
weixin_462446231 天前
macOS 安装 git-lfs
git·macos
吴Wu涛涛涛涛涛Tao1 天前
从单体到子壳:一套「对标亿级 DAU App」的 iOS 架构实战 Demo
ios·架构
linweidong1 天前
网易ios面试题及参考答案(上)
ios·cdn·进程状态·虚拟内存·raii·网络链路·dns系统
摘星编程1 天前
从云游戏到Mac远程操控:UU远程游戏、办公场景全覆盖
游戏·macos·uu远程