View Full Version : How to make UISlider to work as a TIMER

Nov 13, 2009, 10:42 PM

I'm trying to make Custom UISlider to work as a timer gauge like in a game.
So I want the slider to move along as time value decreases every second.

What I have so far is this, but I don't know how to make the UISlider to move every time the value is changed.

UIImage *minImage = [UIImage imageNamed:@"minImage.png"];
UIImage *maxImage = [UIImage imageNamed:@"maxImage.png"];

minImage=[minImage stretchableImageWithLeftCapWidth:13.0 topCapHeight:0.0];
maxImage=[maxImage stretchableImageWithLeftCapWidth:1.0 topCapHeight:0.0];

customSlider = [[UISlider alloc] initWithFrame:CGRectMake(43, 93, 256, 12)];

[customSlider setMinimumTrackImage:minImage forState:UIControlStateNormal];
[customSlider setMaximumTrackImage:maxImage forState:UIControlStateNormal];

customSlider.minimumValue = 0.0;
customSlider.maximumValue = 100.0;
customSlider.continuous = YES;

customSlider.value = 100.0;

[customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];

[self addSubview: customSlider];

- (void)sliderAction:(UISlider *)sender{
UISlider *customSlider = (UISlider *)sender;

float timeValue = customSlider.value;

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(moveTimer) userInfo:nil repeats:YES];


timeValue = timeValue - 1;
if(timeValue <= 0){
timeValue = 0;
[self gameOver];

I would appreciate if anyone can point me to the right direction.

Nov 17, 2009, 11:58 PM
int customSliderValue=100;

CGRect frame = CGRectMake(6.0,422.0,100.0,32.0);
customSlider = [[UISlider alloc] initWithFrame:frame];

customSlider.backgroundColor = [UIColor clearColor];
stetchLeftTrack = [[UIImage imageNamed:@"red.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
stetchRightTrack = [[UIImage imageNamed:@"green.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];

[customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
customSlider.minimumValue = 0.0;
customSlider.maximumValue = 100.0;
customSlider.continuous = YES;
[self.view addSubview:customSlider];

// use a timer to increase the value
timer=[[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(decrementValue) userInfo:nil repeats:YES]retain];



Hope this helps you out

Nov 18, 2009, 01:01 AM
Thank you very much sneha!
I was about to give up and use the Progress View instead,
but using the code you posted, I managed to get the slider work as I wanted.
I really really appreciate your help.

Nov 18, 2009, 07:39 AM