码迷,mamicode.com
首页 > 其他好文 > 详细

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

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

标签:class   位运算   object   方法   位置   返回结果   blog   gre   5.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 --- 《位运算》的使用之二进制颜色的转换(二)

标签:class   位运算   object   方法   位置   返回结果   blog   gre   5.0   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!