在1到100的范围内将UISlider增加1

时间:2012-10-11 07:14:29

标签: ios objective-c cocoa-touch uislider

我是iPhone新手,

如何让我的UISlider以1为增量从1到100?

    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);
}

当我滑动我的日志显示...

slider.value = 1.000000
slider.value = 1.123440
slider.value = 1.234550
slider.value = 1.345670
slider.value = 1.567890
.
.
.

我希望滑块值为1.0,2.0,3.0等等......

任何帮助都会得到满足。

7 个答案:

答案 0 :(得分:13)

//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);
}

答案 1 :(得分:8)

你可以试试这个:

float RoundValue(UISlider * slider) {
  return roundf(slider.value * 2.0) * 1;
}

答案 2 :(得分:7)

yourSlider.minimumValue = 1;
yourSlider.maximumValue = 100;
[yourSlider addTarget:self action:@selector(roundValue) forControlEvents:UIControlEventValueChanged];

- (void)roundValue{
    yourSlider.value = round(yourSlider.value);
}

也看到这个波纹管回答有用的玩具你,给一些想法...

UISlider increments

还有另一个可能会给你一些想法..

UISlider with increments of 5

答案 3 :(得分:1)

更改

` NSLog(@"slider.value=%f",slider.value);`

 NSLog(@"slider.value=%.f",slider.value); 

如果你想要一个数字:

 NSLog(@"slider.value=%.0f",slider.value);

但这会给你一些像1.0,1.1,......的结果。

答案 4 :(得分:0)

以下是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

答案 5 :(得分:0)

当我希望增量为0.5时,我使用这段代码。它适用于1或任何其他数字

- (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);
}

答案 6 :(得分:0)

我知道我迟到了,但最近我也不得不这样做。以下是我使用XCode 8.3.2在swift中完成的方法:

    @IBAction func sliderValueChanged(_ slider: UISlider) {
    label.text = Int(slider.value)
}

Simples!我知道我可以使用'Any'代替'UISlider',但我认为这使我的意图更清晰;)