I am new to iPhone,
How do I have my UISlider go from 1 to 100 in increments of 1
?
slider = [[UISlider alloc] init];
[slider addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
[slider setBackgroundColor:[UIColor clearColor]];
slider.minimumValue = 1;
slider.maximumValue = 100;
slider.continuous = YES;
slider.value = 0.0;
- (IBAction)sliderChange:(id)sender{
NSLog(@"slider.value=%f",slider.value);
}
When i slide my log shows...
slider.value = 1.000000
slider.value = 1.123440
slider.value = 1.234550
slider.value = 1.345670
slider.value = 1.567890
.
.
.
I want slider value as 1.0 , 2.0 , 3.0 and so on...
Any help will be appriciated.
you could try this:
float RoundValue(UISlider * slider) {
return roundf(slider.value * 2.0) * 1;
}
//Only generate update events on release
slider.continuous = NO;
//Round the value and set the slider
- (IBAction)sliderChange:(id)sender
{
int rounded = sender.value; //Casting to an int will truncate, round down
[sender setValue:rounded animated:NO];
NSLog(@"%f", sender.value);
}
yourSlider.minimumValue = 1;
yourSlider.maximumValue = 100;
[yourSlider addTarget:self action:@selector(roundValue) forControlEvents:UIControlEventValueChanged];
- (void)roundValue{
yourSlider.value = round(yourSlider.value);
}
also see this bellow answer helpful toy you,give some idea...
UISlider increments
and also another which may give you some idea..
UISlider with increments of 5
change
` NSLog(@"slider.value=%f",slider.value);`
to
NSLog(@"slider.value=%.f",slider.value);
if you want one digit then :
NSLog(@"slider.value=%.0f",slider.value);
but this will give you results like 1.0,1.1,....
Here is simple Example of UISlider
headereFile.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIScrollViewDelegate,UITextFieldDelegate>
{
IBOutlet UISlider *slider;
IBOutlet UILabel *lbl;
IBOutlet UITextField *txtField;
IBOutlet UIButton *btn;
IBOutlet UISwitch *swich;
IBOutlet UIScrollView *scView;
}
@property(nonatomic,retain)UISlider *slider;
@property(nonatomic,retain)UILabel *lbl;
@property(nonatomic,retain)UITextField *txtField;
@property(nonatomic,retain)UIButton *btn;
@property(nonatomic,retain)UIScrollView *scView;
@property(nonatomic,retain)UISwitch *swich;
-(IBAction)sliderChanged:(UISlider *)slider;
-(IBAction)ButtonPressed:(id)sender;
-(IBAction)showTextField:(id)sender;
@end
implementFile.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize slider,lbl,txtField,btn,scView,swich;
- (void)viewDidLoad
{
self.view.backgroundColor=[UIColor groupTableViewBackgroundColor];
self.scView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,320,460)];
self.scView.delegate=self;
self.scView.contentSize=CGSizeMake(320,465);
[self.view addSubview:self.scView];
self.slider=[[UISlider alloc] initWithFrame:CGRectMake(50,270,220,30)];
self.slider.minimumValue=0.00;
self.slider.maximumValue=100.00;
self.slider.thumbTintColor=[UIColor redColor];
[self.slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
[self.scView addSubview:self.slider];
self.lbl=[[UILabel alloc] initWithFrame:CGRectMake(135, 290,200,30)];
self.lbl.backgroundColor=[UIColor clearColor];
NSString *str=[NSString stringWithFormat:@"%.f",[self.slider value]];
self.lbl.text=str;
[self.scView addSubview:self.lbl];
self.txtField=[[UITextField alloc] initWithFrame:CGRectMake(50, 220,150,30)];
self.txtField.borderStyle=UITextBorderStyleRoundedRect;
self.txtField.delegate=self;
self.txtField.returnKeyType=UIReturnKeyDone;
self.txtField.placeholder=@"Enter 1 to 100";
[self.scView addSubview:self.txtField];
self.btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
self.btn.frame=CGRectMake(205, 215,64, 37);
[self.btn setTitle:@"Change" forState:UIControlStateNormal];
[self.btn addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.scView addSubview:self.btn];
self.swich=[[UISwitch alloc] initWithFrame:CGRectMake(124,140, 75,40)];
[self.swich addTarget:self action:@selector(showTextField:) forControlEvents:UIControlEventValueChanged];
self.swich.on=YES;
/*
((UILabel *)[[[[[[self.swich subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:0]).text = @"Foo";
((UILabel *)[[[[[[self.swich subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:1]).text = @"Bar";
*/
[self.scView addSubview:self.swich];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)showTextField:(id)sender
{
if (self.swich.on)
{
[self.swich setOn:YES animated:YES];
self.txtField.hidden=NO;
self.btn.hidden=NO;
}
else
{
[self.swich setOn:NO animated:YES];
self.txtField.hidden=YES;
self.btn.hidden=YES;
}
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
self.scView.frame=CGRectMake(0, 0,320,230);
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[self.txtField resignFirstResponder];
self.scView.frame=CGRectMake(0, 0,320,460);
return YES;
}
-(IBAction)sliderChanged:(UISlider *)slider;
{
NSString *str=[NSString stringWithFormat:@"%.f",[self.slider value]];
self.lbl.text=str;
}
-(IBAction)ButtonPressed:(id)sender
{
NSString *values=[self.txtField text];
float Values=[values floatValue];
if (Values<=100)
{
self.slider.value=Values;
self.lbl.text=values;
}
else
{
UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:@"NSURLConnection " message:@"Plz...Entre Proper Value !!!" delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[connectFailMessage show];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
I use this bit of code when I want the increment to be 0.5. It works with 1 or any other number
- (void) getSliderValue:(UISlider *)paramSender{
float increment = 0.5;
if ([paramSender isEqual:self.slider]){ //remove if you only have one slider
float newValue = paramSender.value /increment;
paramSender.value = floor(newValue) * increment;
}
NSLog(@"Current value of slider is %f", paramSender.value);
}
I know i'm late to the party but I've also had to do this recently. Here is how I've done it in swift using XCode 8.3.2:
@IBAction func sliderValueChanged(_ slider: UISlider) {
label.text = Int(slider.value)
}
Simples! I know I could have used 'Any' instead of 'UISlider', but I think this makes my intention clearer ;)