Implementation of Slider in iPhone

Introduction

In this article I will create a Single View application. Here I use two buttons (forward and back), two labels (Count_label, Shape_label) and one Slider from outlet. Here we add one control view where we see different shapes. When we perform an action from a button or slider it draws a shape in the control window.

To understand it we use the following.

Step 1

Open XCode by double-clicking on it.

Step 2

Create a New XCode Project by clicking on it.

Step 3

Now select a Single View Application and click on Next.

Step 4

Now provide your Product Name and Company Identifier.

Step 5

Select the location where you want to save your project and click on Create.

Step 6

Now we write the code for each class.


ViewController.h

#import <UIKit/UIKit.h>
#import "drawShapeView.h"

@interface ViewController : UIViewController
{
IBOutlet UILabel *sldrCountlbl,*shapeNamelbl ;
IBOutlet UISlider *sldr;
IBOutlet drawShapeView *shape;
NSTimer *timerForSlider;
Boolean bttnFlag,sldrFlag,flag;
}
-(IBAction)sliderMove:(id)sender;
-(IBAction)back:(id)sender;
-(IBAction)forwd:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
bttnFlag = NO;
sldrFlag = NO;
flag= NO;
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

-(IBAction)back:(id)sender
{
//if(sldrFlag==YES)
{
bttnFlag=YES;
sldrFlag=NO;
flag = NO;
timerForSlider = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];

}
}

-(IBAction)forwd:(id)sender
{
//if(sldrFlag==YES)
{
bttnFlag=YES;
sldrFlag=NO;
flag = YES;
timerForSlider = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];

}

}
-(void)updateSlider

{
//[sldr setValue:sldr.value+1];
if (bttnFlag==YES && sldrFlag==NO)
{
if (flag==YES)
{
if (sldr.value==9)
{
[timerForSlider invalidate];

bttnFlag = NO;

} else
{
sldr.value = sldr.value + (int)1;
[self slider_moveAction];
}
//sldr.value = sldr.value + (int)1;
}
if(flag==NO)
{
if (sldr.value==0)
{
[timerForSlider invalidate];

bttnFlag = NO;

} else
{
sldr.value = sldr.value - (int)1;
[self slider_moveAction];
}

}

}

//sldr.value = sldr.value + (int)1;
/*if(sldr.value==9)
{
[timerForSlider invalidate];

bttnFlag = NO;
}

[self slider_moveAction];
*/
}

-(void)slider_moveAction
{
//if(sldrFlag==YES)
{
NSUInteger vertices = (NSUInteger) sldr.value;
sldrCountlbl.text = [NSString stringWithFormat:@"%d", vertices];
[shape setVertices:vertices];

if ((int)sldr.value ==(int)0)
{
shapeNamelbl.text = @"";
}
if ((int)sldr.value ==(int)1)
{
shapeNamelbl.text = @"Dot";
}
else if ((int)sldr.value ==(int)2)
{
shapeNamelbl.text = @"Line";
}
else if ((int)sldr.value ==(int)3)
{
shapeNamelbl.text = @"Triangle";
}
else if ((int)sldr.value ==(int)4)
{
shapeNamelbl.text = @"Square";
}
else if ((int)sldr.value ==(int)5)
{
shapeNamelbl.text = @"Pentagon";
}
else if ((int)sldr.value ==(int)6)
{
shapeNamelbl.text = @"Hexagon";
}
else if ((int)sldr.value ==(int)7)
{
shapeNamelbl.text = @"Heptagon";
}
else if ((int)sldr.value ==(int)8)
{
shapeNamelbl.text = @"Octagon";
}
}
}

-(IBAction)sliderMove:(id)sender
{
sldrFlag = YES;
bttnFlag = NO;
[self slider_moveAction];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end

drawShapeView.h

#import <UIKit/UIKit.h>

@interface drawShapeView : UIView

- (void)setVertices:(NSUInteger) vertices;
@end

drawShapeView.m

#import "drawShapeView.h"

@implementation drawShapeView
{
NSUInteger _vertices;
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)setVertices:(NSUInteger) vertices
{
_vertices = vertices;
[self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
CGFloat radius = self.bounds.size.width/2.5;
if (radius > self.bounds.size.height/2.5)
{
radius = self.bounds.size.height/2.5;
}

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);

if (_vertices ==(int)1)
{
CGContextClosePath(context);

//
// CGContextFillRect(context, CGRectMake(radius,radius,2,2));
CGContextFillEllipseInRect(context, CGRectMake(135, 135, 2, 2));

}

CGFloat arc = M_PI * 2.0 / _vertices;

CGContextMoveToPoint(context, self.bounds.size.width/2,
self.bounds.size.height/2 - radius);

for (NSUInteger i = 1; i < _vertices; i++)
{
CGContextAddLineToPoint(context,
self.bounds.size.width/2 + radius * sin(arc * i),
self.bounds.size.height/2 - radius * cos(arc * i));
}

CGContextClosePath(context);

CGContextSetLineWidth(context, 5.0);
CGContextStrokePath(context);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/

@end

Step 7

Finally we click on the Run button to show the output.

Step 8

Output 1 in iPhone:

Output1-in-iPhone.jpg

Now we click on the Forward button.

Output 2 in iPhone:

Output2-in-iPhone.jpg

Here increase the count in the label.

Output 3 in iPhone:

Output3-in-iPhone.jpg

Output 4 in iPhone:

Output4-in-iPhone.jpg

Output 5 in iPhone:

Output5-in-iPhone.jpg


Similar Articles