zoukankan      html  css  js  c++  java
  • ios 生成一个动态的随机的头像/随机数的操作

               

    在写项目的时候,可能会遇到这种情况,用到集中随机的颜色,或者头像等,

    首先:把所需要的图片放进一个数组当中

    imgsAry = @[@"t1.png",@"t2.png",@"t3.png",@"t4.png",@"t5.png",@"t6.png",@"t7.png",@"t8.png"];

    然后根据数组的count数生成一个随机数:

    NSInteger randomIndex = arc4random()%imgsAry.count;

    最后就可以直接展示了:             

     [self.headImageView sd_setImageWithURL:[NSURL URLWithString:self.userPhonto] placeholderImage:[UIImage imageNamed:[imgsAry                objectAtIndex:randomIndex]]];

    随机颜色 oc:

    #define GRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]

    swift:

    let GRandomColor = UIColor(red: CGFloat(arc4random_uniform(256)/255), green: CGFloat(arc4random_uniform(256)/255), blue: CGFloat(arc4random_uniform(256)/255), alpha: 1)

    随机数的生成和操作:

    1、  获取一个随机整数范围在:[0,100)包括0,不包括100
    int x = arc4random() % 100;
    2、  获取一个随机数范围在:[500,1000],包括500,包括1000
    int y = (arc4random() % 501) + 500;
    3、  获取一个随机整数,范围在[from,to],包括from,包括to
    -(int)getRandomNumber:(int)from to:(int)to
    {
        return (int)(from + (arc4random() % (to – from + 1)));
    }
  • 相关阅读:
    在你设计中可能用到的20个杂志 PSD 原型
    Gradify
    CamanJS – 提供各种图片处理的 JavaScript 库
    免费素材:包含 250+ 组件的 DO UI Kit
    24个很赞的 Node.js 免费教程和在线指南
    Dynamics.js
    Page Scroll Effects
    Slides
    15款加速 Web 开发的 JavaScript 框架
    Wee – 为现代 Web 开发打造的 CSS 脚手架
  • 原文地址:https://www.cnblogs.com/hero11223/p/5395819.html
Copyright © 2011-2022 走看看