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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12834140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:41:39  来源:igfitidea点击:

Increment UISlider by 1 in range 1 to 100

iosobjective-ccocoa-touchuislider

提问by Krunal

I am new to iPhone,

我是 iPhone 新手,

How do I have my UISlider go from 1 to 100 in increments of 1?

我如何让我的 UISlider 以 1 为增量从 1 到 100 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...

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

Any help will be appriciated.

任何帮助都将得到认可。

采纳答案by Bazinga

you could try this:

你可以试试这个:

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

回答by James Webster

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

回答by Paras Joshi

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

UISlider 增量

and also another which may give you some idea..

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

UISlider with increments of 5

增量为 5 的 UISlider

回答by V-Xtreme

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,....

但这会给你像 1.0,1.1,....

回答by V-Xtreme

Here is simple Example of UISlider

这是一个简单的例子 UISlider

headereFile.h

头文件.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

实现文件.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

回答by HSNN

I use this bit of code when I want the increment to be 0.5. It works with 1 or any other number

当我希望增量为 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);
}

回答by Lazy Dave

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:

我知道我参加聚会迟到了,但我最近也不得不这样做。这是我使用 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 ;)

简单!我知道我可以使用“Any”而不是“UISlider”,但我认为这使我的意图更清晰;)