How did I use UIScrollView in my first iPhone application…

Emergency Light Application turns your iPhone, iPod Touch or iPad in a great source of light. Need to be seen in the dark or in an emergency situation? Don’t worry real light is just one tap away!

• Swipe on the screen to change the light color

• Tap once to enable/disable the siren

• Shake to enable/disable the flash effect

• Tap on the info button to access the settings screen: set the brightness and pick different sound for the siren

How to change colors? Implement UIScrollView! Declare UIScrollView, drag and drop UIScrollView in your NIB file and implemet UIScrollViewDelegate protocol.

@interface ErLightViewController : UIViewController <UIScrollViewDelegate>
{
IBOutlet UIScrollView *scrollView;

}

Initialization of UIScrollView is shown in scrollViewInit method. Here you need to define size of content view. In this case content size is 5 x 320 px.

-(void) scrollViewInit
{
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (int i = 0; i < kNumberOfPages; i++)
[controllers addObject:[NSNull null]];
self.erViewControllers = controllers;
[controllers release];
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
}

Next step is to add different views to scrollView.

– (void)loadScrollViewWithPage:(int)page
{
if (page < 0) return;
if (page >= kNumberOfPages) return;
ViewsController *controller = [erViewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
controller = [[ViewsController alloc] initWithPageNumber:page];
[controller setEr:self];
[erViewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
if (nil == controller.view.superview)
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}

Special thanks to Luka and Ivan who thought me this ;)