Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

yutamic

macrumors newbie
Original poster
Jan 13, 2009
8
0
Hi.

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.
Code:
-(void)createCustomSlider{
	 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;
}

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

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

I would appreciate if anyone can point me to the right direction.
Thanks.
 
Code:
int customSliderValue=100;

-(void)createSlider
{
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;
	customSlider.userInteractionEnabled=FALSE;
[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];

}



-(void)decrementValue
{
     customSliderValue-=1;
	customSlider.value=customSliderValue;
}

Hope this helps you out
 
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.
THANK YOU!!!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.