編集:2年後、この投稿にはまだカルマとコメントがあります。私の古い答えをより良いものに修正しました。
UIColorの入力を0から255の間で取ることができる関数を追加する最も賢明で再利用可能な方法は、カスタムカテゴリを作成することです。読みやすく、デバッグしやすく、他の人が貢献しやすく、ビューのビューコントローラーだけでなく、プロジェクトがきれいになり、構造が維持されます。だから、次のファイルを追加し、あなたが
UIColor + Extra.h
@interface UIColor (Extra)
+ (UIColor *)colorWithR:(uint)red G:(uint)green B:(uint)blue A:(uint) alpha
+ (UIColor *) randomColor;
+ (UIColor *) colorWithHex:(uint) hex;
@end
UIColor + Extra.m
#import "UIColor+Extra.h"
@implementation UIColor (Extra)
+ (UIColor *)colorWithR:(uint)red G:(uint)green B:(uint)blue A:(uint) alpha
{
return [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/100.f];
}
+ (UIColor *) randomColor
{
CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX;
CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
+ (UIColor *) colorWithHex:(uint) hex
{
int red, green, blue, alpha;
blue = hex & 0x000000FF;
green = ((hex & 0x0000FF00) >> 8);
red = ((hex & 0x00FF0000) >> 16);
alpha = ((hex & 0xFF000000) >> 24);
return [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.f];
}
@end
おかげでたくさん私のために働きました。私ができる時、私はこの答えを受け入れるだろう。 – OscarTheGrouch
フォーマットされていないコードサンプルではなく、ちょっとした説明を追加できます。 – jrturton
UIColorは0から1までのプロパティをとります。 – OscarTheGrouch