Wednesday 11 May 2011

Change Background color using UIView

A simple iPhone application with three sliders to change the background color.



It will have three sliders on screen, with Tag 10, 11 and 12, to control the colors of red, green blue. At start-up, all the initiate values are set to 0, also the background will be in black. Whenever any slider's value changed, the values of all the sliders will be read and update UIView.backgroundColor accordingly.

- Create a iPhone Application using View-base Application template, xcodeBackgroundColor.

- Modify xcodeBackgroundColorViewController.h to have a (IBAction)sliderChanged:(id)sender.
#import <UIKit/UIKit.h>

@interface xcodeBackgroundColorViewController : UIViewController {

}
- (IBAction)sliderChanged:(id)sender;
@end
- Modify xcodeBackgroundColorViewController.m to implement (IBAction)sliderChanged:(id)sender.

#import "xcodeBackgroundColorViewController.h"

@implementation xcodeBackgroundColorViewController
- (IBAction)sliderChanged:(id)sender { 
    UISlider *sliderRed = (UISlider *)[self.view viewWithTag:10];
 UISlider *sliderGreen = (UISlider *)[self.view viewWithTag:11];
 UISlider *sliderBlue = (UISlider *)[self.view viewWithTag:12];
 CGFloat red = sliderRed.value;
 CGFloat green = sliderGreen.value;
 CGFloat blue = sliderBlue.value;
 self.view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
} 

Save the works.
- Double click on xcodeBackgroundColorViewController.xib to start Interface Builder.
Click on the View to change the background to Black.


Place three Sliders on the View, set minimum=0.0, maximum=1.0, and initial=0.0. Set Tag=10, 11 and 12 respectively.


Select each Slider on it, connect the IBAction of Value Changed to sliderChanged:

- Open Connection Inspector from Tools -> Connection Inspector, or select the second tag on Inspector.

- Click on the Slider to select it. Drag the circle on the right of Value Changed over the File's Owner in xcodeBackgroundColorViewController.xib.


Release and select ValueChanged.


Repeat on all the three sliders. Save the works.

Build and Go in Xcode.
 
 
Thank you all....!!!

No comments: