Pregunta

I have 15 views with randomly color and I need to sort them by color. I click the sort button on red color and they are arranged from most to least red, and so needs to be done on the green and blue. How can I do this?

It's my 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

And it my 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
¿Fue útil?

Solución

You can get the components of UIColor like this:

CGFloat *components = CGColorGetComponents(color);

Then get the RGB values:

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

Otros consejos

You have to write your own comparison logic. And then you can use 'NSSortDescriptor'

Refer this: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSSortDescriptor_Class/Reference/Reference.html

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top