10

私はiPhoneを初めて使用しますが、

UISliderを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 に答える
1

変化する

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

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

1桁が必要な場合:

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

しかし、これにより 1.0,1.1,... のような結果が得られます。

于 2012-10-11T07:19:15.647 に答える
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

実装ファイル.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 に答える