我有15个随机颜色的视图,我需要按颜色对它们进行排序。我点击红色的排序按钮,它们从最红到最少排列,所以需要在绿色和蓝色上完成。我怎么能这样做?
这是我的ViewController
//
// ViewController.m
// FewView
//
// Created by admin on 3/11/14.
// Copyright (c) 2014 admin. All rights reserved.
//
#import "ViewController.h"
#import "RandomView.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize kol;
@synthesize sdvig1;
@synthesize sdvig2;
@synthesize myTextField;
@synthesize randView;
@synthesize redButton, greenButton, blueButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
kol=10;
}
return self;
}
-(void) viewWillAppear:(BOOL)animated{
myTextField.delegate = self;
kol=[[myTextField text] intValue];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([self.myTextField.text isEqualToString:@""]) {
} else
{
[self.myTextField resignFirstResponder];
kol=[[self.myTextField text] intValue];
[self removeView];
[self createView];
}
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width/4, 30)];
myTextField.backgroundColor=[UIColor yellowColor];
[myTextField setKeyboardType:UIKeyboardTypeNumberPad];
[self.view addSubview:myTextField];
redButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4, 20, self.view.frame.size.width/4, 30)];
redButton.backgroundColor=[UIColor redColor];
[redButton addTarget: self
action: @selector(redSort:) forControlEvents: UIControlEventTouchUpInside];
greenButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4*2, 20, self.view.frame.size.width/4, 30)];
greenButton.backgroundColor=[UIColor greenColor];
[greenButton addTarget: self
action: @selector(greenSort:) forControlEvents: UIControlEventTouchUpInside];
blueButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4*3, 20, self.view.frame.size.width/4, 30)];
blueButton.backgroundColor=[UIColor blueColor];
[blueButton addTarget: self
action: @selector(blueSort:) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:redButton];
[self.view addSubview:greenButton];
[self.view addSubview:blueButton];
[self createView];
}
-(void)createView{
sdvig1=self.view.frame.size.width/(2*(kol+5));
sdvig2=self.view.frame.size.height/(2*(kol+5));
randView = [[RandomView alloc] initWithFrame:CGRectMake(0,self.myTextField.frame.origin.y+self.myTextField.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-(self.myTextField.frame.origin.y+self.myTextField.frame.size.height)) count:kol sdvig:CGPointMake(sdvig1, sdvig2) vc:self];
[self.view addSubview:randView];
}
-(void) removeView{
[randView removeFromSuperview];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[myTextField resignFirstResponder];
[self textFieldShouldReturn:myTextField];
myTextField.text = @"";
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(IBAction)redSort:(id)sender{
NSLog(@"red");
}
-(IBAction)greenSort:(id)sender{
NSLog(@"gren");
}
-(IBAction)blueSort:(id)sender{
NSLog(@"blue");
}
//-(void) sortMyView{
// // randView = [[RandomView alloc] init];
// [randView.myArray sortUsingComparator:^( obj1, randView obj2)]{
// // сравниваем два объекта
// // и возвращаем YES или NO
// return YES;
// }];
//}
-(UIColor *) randomColor
{
CGFloat red = (CGFloat)arc4random() / (CGFloat)RAND_MAX;
CGFloat blue = (CGFloat)arc4random() / (CGFloat)RAND_MAX;
CGFloat green = (CGFloat)arc4random() / (CGFloat)RAND_MAX;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
@end
它是我的View Class
//
// RandomView.m
// FewView
//
// Created by admin on 3/11/14.
// Copyright (c) 2014 admin. All rights reserved.
//
#import "RandomView.h"
#import "ViewController.h"
@implementation RandomView
@synthesize randView;
@synthesize myArray;
- (id)initWithFrame:(CGRect)frame count:(NSInteger)kol sdvig:(CGPoint)sdvig vc:(ViewController*) delegat
{
self = [super initWithFrame:frame];
if (self)
{
if (kol>0) {
[self setBackgroundColor:[delegat randomColor]];
randView = [[RandomView alloc] initWithFrame:CGRectMake(sdvig.x, sdvig.y, self. frame.size.width-2*sdvig.x, self.frame.size.height-2*sdvig.y) count:--kol sdvig:CGPointMake(sdvig.x, sdvig.y) vc:delegat];
[self addSubview:randView];
myArray = [[NSMutableArray alloc]init];
[myArray addObject:randView];
NSLog(@"color %@", delegat.randomColor);
// NSLog(@"obj %@", myArray);
// NSLog(@"view= %@", randView);
}
}
return self;
}
@end
答案 0 :(得分:1)
您可以像这样获取UIColor的组件:
CGFloat *components = CGColorGetComponents(color);
然后获取RGB值:
components[0],components[1],components[2]
答案 1 :(得分:0)
你必须编写自己的比较逻辑。然后你可以使用'NSSortDescriptor'