Click here to Skip to main content
15,895,809 members
Articles / Mobile Apps / iPhone

Switch between views Mobile Safari-style

Rate me:
Please Sign up or sign in to vote.
4.96/5 (34 votes)
23 Jul 2010CPOL15 min read 81K   661   20  
A reusable library (specifically, an UIViewController subclass) to implement Mobile Safari page/tab switching interface in your own app. Now supports orientation changes!
//
//  LSDotLayer.m
//  ViewSwitcher
//
//  Created by Le Son on 6/10/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "LSDotLayer.h"

@implementation LSDotLayer

@dynamic numberOfDots;
@dynamic activeDot;

- (NSUInteger)numberOfDots
{
	return numberOfDots;
}

- (void)setNumberOfDots:(NSUInteger)n
{
	numberOfDots = n;
	[self setNeedsDisplay];
}

- (NSUInteger)activeDot
{
	return activeDot;
}

- (void)setActiveDot:(NSUInteger)n
{
	activeDot = n;
	[self setNeedsDisplay];
}

- (void)drawInContext:(CGContextRef)ctx
{
	if (numberOfDots > 1)
	{
		// Diameter = height of this layer
		float diameter = self.bounds.size.height;
		
		// Get center point
		CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
		
		// Get point of first dot
		float x = center.x - (diameter + 10.0f) * self.numberOfDots/2 + 10.0f;
	
		float y = center.y;
		CGPoint pos = CGPointMake(x, y);
	
		for (int i = 0; i < self.numberOfDots; i++)
		{
			// Referenced from http://alanduncan.net/old/index.php?q=node/4
			
			// Draw the dot
			CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, (i == self.activeDot ? 1.0f : 0.5f));
			float radius = diameter * 0.5;
			CGRect myOval = CGRectMake(pos.x - radius, pos.y - radius, diameter, diameter);
			CGContextAddEllipseInRect(ctx, myOval);
			CGContextFillPath(ctx);
			CGContextStrokePath(ctx);
			
			// Increase pos
			pos.x += diameter + 10.0f;
		}
	}
}

@end

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other Hanoi - Amsterdam High School
Vietnam Vietnam
I'm a 16 year old high school student. I know some about Objective-C and a bit about C#, mostly through reading stuff online.

Comments and Discussions