uibutton

view.h
#import <UIKit/UIKit.h>


@interface ViewController : UIViewController
@property UILabel * redLabel,* greenLabel, * blueLabel,*mainLabel,*sLabel;
@property UITextField *redText,*greenText,*blueText;
@property UISlider * redSlider, * greenSlider, * blueSlider, *mainSlider;
@property UIView * mainView;
@property UISwitch * mainSwitch;
@property UIButton * addButton,*mainButton,*subButton;
@property NSMutableArray * myArray;

@end

view.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
int x=30,y=300;
int a=30,b=350;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"11.jpg"]];
    self.myArray=[[NSMutableArray alloc]init];
    [self label];
    [self slider];
    [self text];
   
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)label
{
    self.addButton=[UIButton buttonWithType:UIButtonTypeCustom];
    self.addButton.frame= CGRectMake(280, 250, 50, 30);
    self.addButton.backgroundColor=[UIColor orangeColor];
    [self.addButton setTitleShadowColor:[UIColor brownColor] forState:UIControlStateNormal];
    [self.addButton setImage:[UIImage imageNamed:@"add.jpeg"] forState:UIControlStateNormal];
    [self.view addSubview:self.addButton];
    [self.addButton addTarget:self action:@selector(addBtn:) forControlEvents:UIControlEventTouchUpInside];

    
    self.mainView=[[UIView alloc]initWithFrame:CGRectMake(250, 100, 120, 120)];
    self.mainView.backgroundColor=[UIColor blackColor];
    [self.view addSubview:self.mainView];
    
    self.mainSwitch=[[UISwitch alloc]initWithFrame:CGRectMake(200, 50, 0, 0)];
    [self.view addSubview:self.mainSwitch];
    
    self.redLabel= [[UILabel alloc]initWithFrame:CGRectMake(30, 100, 30, 30)];
    self.redLabel.text=@"R";
    [self.view addSubview:self.redLabel];
    
    self.greenLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 150, 30, 30)];
    self.greenLabel.text=@"G";
    [self.view addSubview:self.greenLabel];
    
    self.blueLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 200, 30, 30)];
    self.blueLabel.text=@"B";
    [self.view addSubview:self.blueLabel];
    
    self.mainLabel=[[UILabel alloc]initWithFrame:CGRectMake(80, 50, 50, 30)];
    self.mainLabel.text=@"H/V";
    [self.view addSubview:self.mainLabel];
    
    self.sLabel=[[UILabel alloc]initWithFrame:CGRectMake(190, 250, 50, 30)];
    self.sLabel.backgroundColor=[UIColor cyanColor];
    [self.view addSubview:self.sLabel];
    
     }

-(void)slider
{
    self.redSlider=[[UISlider alloc]initWithFrame:CGRectMake(80, 100, 100, 30)];
    self.redSlider.minimumValue=0;
    self.redSlider.maximumValue=255;
    [self.redSlider addTarget:self action:@selector(rgbView:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.redSlider];
    
    self.greenSlider=[[UISlider alloc]initWithFrame:CGRectMake(80, 150, 100, 30)];
    self.greenSlider.minimumValue=0;
    self.greenSlider.maximumValue=255;
    [self.greenSlider addTarget:self action:@selector(rgbView:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.greenSlider];
    
    self.blueSlider = [[UISlider alloc]initWithFrame:CGRectMake(80, 200, 100, 30)];
    self.blueSlider.minimumValue=0;
    self.blueSlider.maximumValue=255;
    [self.blueSlider addTarget:self action:@selector(rgbView:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.blueSlider];
    
    self.mainSlider=[[UISlider alloc]initWithFrame:CGRectMake(80, 250, 100, 30)];
    self.mainSlider.minimumValue=0;
    self.mainSlider.maximumValue=50;
    [self.mainSlider addTarget:self action:@selector(buttonView:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.mainSlider];
    
}

-(void)buttonView:(id)sender
{
    [self remove];
    self.sLabel.text=[NSString stringWithFormat:@"%i",(int)self.mainSlider.value];
}

-(void)text
{
    self.redText=[[UITextField alloc]initWithFrame:CGRectMake(190, 100, 50, 30)];
    self.redText.borderStyle= UITextBorderStyleBezel;
    [self.view addSubview:self.redText];
    
    self.greenText=[[UITextField alloc]initWithFrame:CGRectMake(190, 150, 50, 30)];
    self.greenText.borderStyle=UITextBorderStyleBezel;
    [self.view addSubview:self.greenText];
    
    self.blueText=[[UITextField alloc]initWithFrame:CGRectMake(190, 200, 50, 30)];
    self.blueText.borderStyle=UITextBorderStyleBezel;
    [self.view addSubview:self.blueText];
    
  
    
}

-(void)rgbView:(UISlider *)sender
{
    self.redText.text=[NSString stringWithFormat:@"%i",(int)self.redSlider.value];
    self.greenText.text=[NSString stringWithFormat:@"%i",(int)self.greenSlider.value];
    self.blueText.text=[NSString stringWithFormat:@"%i",(int)self.blueSlider.value];
    
    self.mainView.backgroundColor=[[UIColor alloc]initWithRed:self.redSlider.value/255.f green:self.greenSlider.value/255.f blue:self.blueSlider.value/255.f alpha:1.0];
    
}

-(void)addBtn:(UIButton *)sender
{
    
    self.mainButton = [UIButton buttonWithType:UIButtonTypeCustom];
       self.mainButton.frame  =CGRectMake(x, y, 30, 30);
    
    [self.mainButton setTitle:[NSString stringWithFormat:@"%i",(int)self.mainSlider.value] forState:UIControlStateNormal];
//    self.mainButton.backgroundColor = [UIColor blueColor];
    [self.mainButton setBackgroundImage:[UIImage imageNamed:@"20.png"] forState:UIControlStateNormal];
    [self.mainButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [self.mainButton addTarget:self action:@selector(operations:) forControlEvents:UIControlEventTouchUpInside];
    x=x+50;
    if (x>300) {
        x=30;
        y=y+50;
    }
//
    [self.view addSubview:self.mainButton];
//    [self.myArray addObject:self.mainButton];

}

-(void)operations:(UIButton *)sender
{
    if (self.mainSwitch.on==YES)
    {
        
   [self remove];
    for (int i=1; i <=self.mainSlider.value; i++)
    {
    
        self.subButton= [UIButton buttonWithType:UIButtonTypeSystem];
        self.subButton.frame=CGRectMake(a, b30, 30);
        [self.subButton addTarget:self action:@selector(operationDelete:) forControlEvents:UIControlEventTouchUpInside];
        self.subButton.backgroundColor=[[UIColor alloc]initWithRed:self.redSlider.value green:self.greenSlider.value blue:self.blueSlider.value alpha:1.0];
        [self.subButton setTitle:[NSString stringWithFormat:@"%i",i] forState:UIControlStateNormal];
        a=a+40;
        if (a>350)
        {
            a=30;
            b=b+40;
        }
    [self.view addSubview:self.subButton];
    [self.myArray addObject:self.subButton];
    }
}
    else if (self.mainSwitch.on==NO)
    {
        [self remove];
        for (int i=1; i <=self.mainSlider.value; i++) {
            
            self.subButton= [UIButton buttonWithType:UIButtonTypeSystem];
            self.subButton.frame=CGRectMake(a, b30, 30);
            [self.subButton addTarget:self action:@selector(operationDelete:) forControlEvents:UIControlEventTouchUpInside];
            self.subButton.backgroundColor=[[UIColor alloc]initWithRed:self.redSlider.value green:self.greenSlider.value blue:self.blueSlider.value alpha:1.0];
            [self.subButton setTitle:[NSString stringWithFormat:@"%i",i] forState:UIControlStateNormal];
            b=b+40;
            if (b>600) {
                a=a+40;
                b=350;
                
            }
            [self.view addSubview:self.subButton];
            [self.myArray addObject:self.subButton];
            
        }
        
    }
    
}
-(void)remove
{
    for (int i=0; i< self.myArray.count; i++) {
        UIButton * btn = [self.myArray objectAtIndex:i];
        [btn removeFromSuperview];
        a=30;
        b=350;
    }
    
}
-(void)operationDelete:(UIButton *)sender
{
    [self remove];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
Previous
Next Post »