2014-03-14 2 views
-4

У меня 15 просмотров со случайным цветом, и мне нужно отсортировать их по цвету. Я нажимаю кнопку сортировки по красному цвету, и они расположены от большинства до красных, поэтому это нужно сделать на зеленом и синем. Как я могу это сделать?Как я могу сортировать по вертикали 15 по цвету ios

Это мой 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 

И это мой взгляд Класс

// 
// 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 
+2

что ты уже испробовал? Что он сделал неправильно? Рассматриваете ли вы только один компонент цвета в любом сравнении? – Wain

ответ

1

Вы можете получить компоненты UIColor так:

CGFloat *components = CGColorGetComponents(color); 

Затем получить значения RGB:

components[0],components[1],components[2] 
+0

Я не знаю, как я могу это сделать. Можете ли вы показать мне какой-нибудь пример? – user3419453

Смежные вопросы