viewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property UISegmentedControl *elementCategories,*positionCategories,*dynamicSegment;
@property UISlider *redSlider,*greenSlider,*blueSlider,*valueSlider;
@property UILabel *redLabel,*greenLabel,*blueLabel,*label;
@property UITextField *redText,*greenText,*blueText,*sliderText,*buttonText;;
@property UIView *colorView;
@property UIButton *addButton,*textFieldButton,*btn,*resetButton;
@property NSMutableArray *array;
@property UIScrollView *scrollView;
@property UISwitch *displaySwitch;
@property NSArray *sortSegments;
@end
viewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
int xPos = 40 , yPos = 40;
int i = 0;
- (void)viewDidLoad {
self.array = [[NSMutableArray alloc]init];
self.sortSegments = [[NSArray alloc]init];
[super viewDidLoad];
[self createUIElements];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)createUIElements
{
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.jpg"]];
// self.view.backgroundColor = [UIColor grayColor];
self.elementCategories = [[UISegmentedControl alloc]initWithItems:@[@"Button",@"Label",@"Switch"]];
self.elementCategories.frame = CGRectMake(50, 30, 250, 30);
[self.elementCategories setTintColor:[UIColor blackColor]];
[self.view addSubview:self.elementCategories];
// [self.elementCategories addTarget:self action:@selector(onElementSelection) forControlEvents:UIControlEventValueChanged];
self.positionCategories = [[UISegmentedControl alloc]initWithItems:@[@"Horizontal",@"Vertical",@"ZigZag"]];
self.positionCategories.frame = CGRectMake(50, 70, 250, 30);
[self.positionCategories setTintColor:[UIColor blackColor]];
[self.view addSubview:self.positionCategories];
self.resetButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.resetButton.frame = CGRectMake(330, 70, 50, 30);
[self.resetButton.layer setBorderWidth:1];
[self.resetButton setTintColor:[UIColor blackColor]];
[self.resetButton setTitle:@"RESET" forState:UIControlStateNormal];
[self.view addSubview:self.resetButton];
[self.resetButton addTarget:self action:@selector(resetAction) forControlEvents:UIControlEventTouchUpInside];
self.redLabel = [[UILabel alloc]init];
self.redLabel.frame = CGRectMake(20, 120, 25, 25);
self.redLabel.text = @"R";
[self.view addSubview:self.redLabel];
self.greenLabel = [[UILabel alloc]init];
self.greenLabel.frame = CGRectMake(20, 170, 25, 25);
self.greenLabel.text = @"G";
[self.view addSubview:self.greenLabel];
self.blueLabel = [[UILabel alloc]init];
self.blueLabel.frame = CGRectMake(20, 220, 25, 25);
self.blueLabel.text = @"B";
[self.view addSubview:self.blueLabel];
self.redText = [[UITextField alloc]init];
self.redText.frame = CGRectMake(180, 120, 50, 30);
self.redText.borderStyle = UITextBorderStyleBezel;
self.redText.layer.borderWidth = 2;
self.redText.layer.cornerRadius=5.0f;
self.redText.layer.masksToBounds=YES;
self.redText.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview:self.redText];
self.greenText = [[UITextField alloc]init];
self.greenText.frame = CGRectMake(180, 170, 50, 30);
self.greenText.borderStyle = UITextBorderStyleBezel;
self.greenText.layer.borderWidth = 2;
self.greenText.layer.cornerRadius=5.0f;
self.greenText.layer.masksToBounds=YES;
self.greenText.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview:self.greenText];
self.blueText = [[UITextField alloc]init];
self.blueText.frame = CGRectMake(180, 220, 50, 30);
self.blueText.borderStyle = UITextBorderStyleBezel;
self.blueText.layer.borderWidth = 2;
self.blueText.layer.cornerRadius=5.0f;
self.blueText.layer.masksToBounds=YES;
self.blueText.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview:self.blueText];
self.sliderText = [[UITextField alloc]init];
self.sliderText.frame = CGRectMake(230, 270, 60, 30);
self.sliderText.borderStyle = UITextBorderStyleBezel;
self.sliderText.layer.borderWidth = 2;
self.sliderText.layer.cornerRadius=5.0f;
self.sliderText.layer.masksToBounds=YES;
self.sliderText.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview:self.sliderText];
self.redSlider = [[UISlider alloc]init];
self.redSlider.frame = CGRectMake(40, 120, 125, 25);
[self.view addSubview:self.redSlider];
self.redSlider.minimumValue = 0;
self.redSlider.maximumValue = 255;
[self.redSlider setTintColor:[UIColor redColor]];
self.redSlider.thumbTintColor=[UIColor redColor];
[self.redSlider addTarget:self action:@selector(createRGBView:) forControlEvents:UIControlEventValueChanged];
// [self.redSlider addTarget:self action:@selector(onElementSelection) forControlEvents:UIControlEventValueChanged];
self.greenSlider = [[UISlider alloc]init];
self.greenSlider.frame = CGRectMake(40, 170, 125, 25);
[self.view addSubview:self.greenSlider];
self.greenSlider.minimumValue = 0;
self.greenSlider.maximumValue = 255;
[self.greenSlider setTintColor:[UIColor greenColor]];
self.greenSlider.thumbTintColor=[UIColor greenColor];
[self.greenSlider addTarget:self action:@selector(createRGBView:) forControlEvents:UIControlEventValueChanged];
// [self.greenSlider addTarget:self action:@selector(onElementSelection) forControlEvents:UIControlEventValueChanged];
self.blueSlider = [[UISlider alloc]init];
self.blueSlider.frame = CGRectMake(40, 220, 125, 25);
[self.view addSubview:self.blueSlider];
self.blueSlider.minimumValue = 0;
self.blueSlider.maximumValue = 255;
[self.blueSlider setTintColor:[UIColor blueColor]];
self.blueSlider.thumbTintColor=[UIColor blueColor];
[self.blueSlider addTarget:self action:@selector(createRGBView:) forControlEvents:UIControlEventValueChanged];
// [self.blueSlider addTarget:self action:@selector(onElementSelection) forControlEvents:UIControlEventValueChanged];
self.valueSlider = [[UISlider alloc]init];
self.valueSlider.frame = CGRectMake(40, 270, 180, 25);
[self.view addSubview:self.valueSlider];
self.valueSlider.minimumValue = 0;
self.valueSlider.maximumValue = 2000;
self.valueSlider.continuous = YES;
// [self.valueSlider setThumbImage:[UIImage imageNamed:@"slider.png" ] forState:UIControlStateNormal];
[self.valueSlider addTarget:self action:@selector(createRGBView:) forControlEvents:UIControlEventValueChanged];
self.colorView = [[UIView alloc]init];
self.colorView.frame = CGRectMake(280,120, 120 , 130);
[self.view addSubview:self.colorView];
self.addButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.addButton.frame = CGRectMake(300, 270, 40, 30);
[self.addButton setTitle:@"ADD" forState:UIControlStateNormal];
[self.addButton.layer setBorderWidth:1];
[self.addButton setTintColor:[UIColor blackColor]];
[self.view addSubview:self.addButton];
[self.addButton addTarget:self action:@selector(onAddBtnTap) forControlEvents:UIControlEventTouchUpInside];
self.buttonText = [[UITextField alloc]init];
self.buttonText.frame = CGRectMake(40 , 330 , 80, 30);
self.buttonText.borderStyle = UITextBorderStyleBezel;
self.buttonText.layer.borderWidth = 2;
self.buttonText.layer.cornerRadius=8.0f;
self.buttonText.layer.masksToBounds=YES;
self.buttonText.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview:self.buttonText];
self.textFieldButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.textFieldButton.frame = CGRectMake(150 , 330, 55, 30);
[self.textFieldButton setTitle:@"ENTER" forState:UIControlStateNormal];
[self.textFieldButton.layer setBorderWidth:1];
[self.textFieldButton setTintColor:[UIColor blackColor]];
[self.view addSubview:self.textFieldButton];
[self.textFieldButton addTarget:self action:@selector(onEnterBtnTap) forControlEvents:UIControlEventTouchUpInside];
self.dynamicSegment = [[UISegmentedControl alloc]init];
self.dynamicSegment.frame = CGRectMake(40, 380, 300, 40);
[self.dynamicSegment setTintColor:[UIColor blackColor]];
[self.view addSubview:self.dynamicSegment];
[self.dynamicSegment addTarget:self action:@selector(onElementSelection) forControlEvents:UIControlEventValueChanged];
self.scrollView = [[UIScrollView alloc]init];
self.scrollView.frame = CGRectMake(0, 430,[[UIScreen mainScreen]bounds].size.width , 270);
self.scrollView.backgroundColor = [UIColor brownColor];
self.scrollView.layer.borderColor =[UIColor redColor].CGColor ;
self.scrollView.layer.borderWidth = 1;
self.scrollView.contentSize = CGSizeMake([[UIScreen mainScreen]bounds].size.width, 10000);
[self.view addSubview:self.scrollView];
}
-(void)createRGBView:(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.sliderText.text = [NSString stringWithFormat:@"%i",(int)self.valueSlider.value];
self.colorView.backgroundColor = [[UIColor alloc]initWithRed:self.redSlider.value/255.0 green:self.greenSlider.value/255.0 blue:self.blueSlider.value/255.0 alpha:1.0];
}
-(void)onAddBtnTap
{
i++;
[self.array addObject:[NSString stringWithFormat:@"%i",(int)self.valueSlider.value]];
self.sortSegments = [self.array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2 options:NSNumericSearch];
}];
[self removeSegment];
for(int j = 0 ; j < self.sortSegments.count ; j++)
{
[self.dynamicSegment insertSegmentWithTitle:[NSString stringWithFormat:@"%@" ,[self.sortSegments objectAtIndex:j]] atIndex: i animated:YES];
NSLog(@"%lu",self.sortSegments.count);
}
}
-(void)onEnterBtnTap
{
i++;
NSString *textStoreValue = self.buttonText.text;
[self.array addObject:[NSString stringWithFormat:@"%@",textStoreValue]];
NSArray *sortSegments = [self.array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2 options:NSNumericSearch];
}];
[self removeSegment];
for(int j = 0 ; j < sortSegments.count ; j++)
{
[self.dynamicSegment insertSegmentWithTitle:[NSString stringWithFormat:@"%@" ,[sortSegments objectAtIndex:j]] atIndex: i animated:YES];
}
}
-(void)removeSegment
{
[self.dynamicSegment removeAllSegments];
}
-(void)onElementSelection
{
[self removeBtn];
if(self.elementCategories.selectedSegmentIndex == 0)
{
bool isDecrement = NO;
NSLog(@"%li",(long)self.dynamicSegment.selectedSegmentIndex);
NSString *title = [self.dynamicSegment titleForSegmentAtIndex:self.dynamicSegment.selectedSegmentIndex];
for(int i = 1 ; i <=[title intValue]; i++)
{
// NSLog(@"%@",sender.currentTitle);
self.btn = [UIButton buttonWithType:UIButtonTypeSystem];
self.btn.frame = CGRectMake(xPos, yPos, 30, 25);
[self.btn setTitle: [NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
[self.btn.layer setBorderWidth:1];
[self.btn.layer setBorderColor:[[UIColor blackColor] CGColor]];
self.btn.tintColor =[[UIColor alloc]initWithRed:self.redSlider.value/255.0 green:self.greenSlider.value/255.0 blue:self.blueSlider.value/255.0 alpha:1.0];
[self.scrollView addSubview:self.btn];
if(self.positionCategories.selectedSegmentIndex ==0)
{
xPos = xPos + 40;
if(xPos>320)
{
yPos = yPos + 50;
xPos = 40;
}
}
else if(self.positionCategories.selectedSegmentIndex ==1)
{
yPos = yPos + 40;
if(yPos>9800)
{
xPos = xPos + 50;
yPos = 40;
}
}
else if (self.positionCategories.selectedSegmentIndex ==2)
{
if(isDecrement == NO)
{
xPos=xPos+40;
}
else
{
xPos = xPos - 40;
}
if (xPos >=320)
{
isDecrement = YES;
yPos = yPos + 40;
xPos = xPos - 40;
}
if (xPos<40)
{
xPos =xPos + 40;
yPos = yPos + 40;
isDecrement = NO;
}
}
[self.scrollView addSubview:self.btn];
}
}
if(self.elementCategories.selectedSegmentIndex == 1)
{
bool isDecrement = NO;
NSLog(@"%li",(long)self.dynamicSegment.selectedSegmentIndex);
NSString *title = [self.dynamicSegment titleForSegmentAtIndex:self.dynamicSegment.selectedSegmentIndex];
for(int i = 1 ; i <=[title intValue]; i++)
{
// NSLog(@"%@",sender.currentTitle);
self.label = [[UILabel alloc]init];
self.label.frame = CGRectMake(xPos, yPos, 35, 25);
self.label.text =[NSString stringWithFormat:@"%d",i];
self.label.textColor = [[UIColor alloc]initWithRed:self.redSlider.value/255.0 green:self.greenSlider.value/255.0 blue:self.blueSlider.value/255.0 alpha:1.0];
[self.scrollView addSubview:self.label];
if(self.positionCategories.selectedSegmentIndex ==0)
{
xPos = xPos + 40;
if(xPos>320)
{
yPos = yPos + 50;
xPos = 40;
}
}
else if(self.positionCategories.selectedSegmentIndex ==1)
{
yPos = yPos + 40;
if(yPos>9800)
{
xPos = xPos + 50;
yPos = 40;
}
}
else if (self.positionCategories.selectedSegmentIndex ==2)
{
if(isDecrement == NO)
{
xPos=xPos+40;
}
else
{
xPos = xPos - 40;
}
if (xPos >=320)
{
isDecrement = YES;
yPos = yPos + 40;
xPos = xPos - 40;
}
if (xPos<40)
{
xPos =xPos + 40;
yPos = yPos + 40;
isDecrement = NO;
}
}
[self.scrollView addSubview:self.label];
}
}
if(self.elementCategories.selectedSegmentIndex == 2)
{
BOOL isDecrement = NO;
NSLog(@"%li",(long)self.dynamicSegment.selectedSegmentIndex);
NSString *title = [self.dynamicSegment titleForSegmentAtIndex:self.dynamicSegment.selectedSegmentIndex];
for(int i = 1 ; i <=[title intValue]; i++)
{
// NSLog(@"%@",sender.currentTitle);
self.displaySwitch = [[UISwitch alloc]init];
self.displaySwitch.frame = CGRectMake(xPos, yPos, 25, 25);
self.displaySwitch.tintColor = [[UIColor alloc]initWithRed:self.redSlider.value/255.0 green:self.greenSlider.value/255.0 blue:self.blueSlider.value/255.0 alpha:1.0];
[self.scrollView addSubview:self.displaySwitch];
// self.label.text =[NSString stringWithFormat:@"%d",i];
if(self.positionCategories.selectedSegmentIndex ==0)
{
xPos = xPos + 50;
if(xPos>320)
{
yPos = yPos + 50;
xPos = 40;
}
}
else if(self.positionCategories.selectedSegmentIndex ==1)
{
yPos = yPos + 40;
if(yPos>9800)
{
xPos = xPos + 50;
yPos = 40;
}
}
else if (self.positionCategories.selectedSegmentIndex ==2)
{
if(isDecrement == NO)
{
xPos=xPos+50;
}
else
{
xPos = xPos - 50;
}
if (xPos >=320)
{
isDecrement = YES;
yPos = yPos + 40;
xPos = xPos -50;
}
if (xPos<40)
{
xPos =xPos + 50;
yPos = yPos + 40;
isDecrement = NO;
}
}
[self.scrollView addSubview:self.displaySwitch];
}
}
}
-(void)removeBtn
{
for(UIView *v in [self.scrollView subviews])
{
[v removeFromSuperview];
}
xPos= 40 ;
yPos = 40;
}
-(void)resetAction
{
[self.dynamicSegment removeAllSegments];
for ( UIView *v in [self.scrollView subviews])
{
[v removeFromSuperview];
}
self.sliderText.text = 0;
self.buttonText.text = 0;
self.valueSlider.value = 0;
[self.elementCategories setSelectedSegmentIndex:UISegmentedControlNoSegment];
[self.positionCategories setSelectedSegmentIndex:UISegmentedControlNoSegment];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up here with your email
ConversionConversion EmoticonEmoticon