10

我是 iPhone 新手,

如何让我的 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);
}

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

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

4

7 回答 7

14
//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);
}
于 2012-10-11T07:26:42.500 回答
8

你可以试试这个:

float RoundValue(UISlider * slider) {
  return roundf(slider.value * 2.0) * 1;
}
于 2012-10-11T07:22:38.970 回答
7
yourSlider.minimumValue = 1;
yourSlider.maximumValue = 100;
[yourSlider addTarget:self action:@selector(roundValue) forControlEvents:UIControlEventValueChanged];

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

另请参阅下面的答案对您有帮助的玩具,请给出一些想法...

UISlider 增量

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

增量为 5 的 UISlider

于 2012-10-11T07:21:28.827 回答
2

我知道我参加聚会迟到了,但我最近也不得不这样做。这是我使用 XCode 8.3.2 快速完成的方法:

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

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

于 2017-06-24T08:28:46.540 回答
1

改变

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

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

如果你想要一位数字,那么:

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

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

于 2012-10-11T07:19:15.647 回答
0

这是一个简单的例子UISlider

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

实现文件.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
于 2012-10-11T07:26:30.767 回答
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);
}
于 2014-09-05T13:47:26.157 回答