首页 > 其他 > 详细

objective-C和C --- 《位运算》的使用之二进制颜色的转换(二)

时间:2017-03-14 22:38:58      阅读:218      评论:0      收藏:0      [点我收藏+]

为UIColor写个分类方法

+ (UIColor *)colorWithBinaryHex:(u_int32_t)hex{
    //0x(hex)0000
    int red = (0xFF0000 & hex) >> 16;     //返回还是hex,在FF的位置上,从左数第5位,也就是2的4次方
    int green = (0x00FF00 & hex) >> 8; //返回还是hex,在FF的位置上
    // hex => 0x 0000AA   => 00001010 1010  //返回还是hex,在FF的位置上,个位上
    // 与上 => 0x 0000FF   => 00001111 1111  => 结果还是:000010101010 =>0x0000AA
    int blue = (0x0000FF & hex);
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
}

 //调用它

self.view.backgroundColor = [UIColor colorWithBinaryHex:0xFF0000];
//返回结果是红色

 

objective-C和C --- 《位运算》的使用之二进制颜色的转换(二)

原文:http://www.cnblogs.com/TheYouth/p/6550994.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!