Objective-C Learning Notes - UISlider
base attributes:
minimumValue;
maximumValue;
UIImage.
动态实现:
#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize myLabel; - (void)viewDidLoad { [super viewDidLoad]; //myLabel来源于. h 文件 myLabel = [[UILabel alloc]init]; //frame 赋值 myLabel.frame = self.view.bounds; // myLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; //初始值 myLabel.text = @"50.0"; //字体 myLabel.font = [UIFont boldSystemFontOfSize:36]; //对齐方式 myLabel.textAlignment = NSTextAlignmentCenter; //添加到 view 中 [self.view addSubview:myLabel]; //创建 UISlider 实例 UISlider *mySlider = [[UISlider alloc]init]; //frame 赋值 mySlider.frame = CGRectMake(0, 0, 250, 50); //最小值 mySlider.minimumValue = 0.0; //最大值 mySlider.maximumValue = 100.0; //初始值 mySlider.value = 50.0; //中心对齐? mySlider.center = self.view.center; //触发 [mySlider addTarget:self action:@selector(sliderDidChange:) forControlEvents:UIControlEventValueChanged]; //添加到 view [self.view addSubview:mySlider]; // Do any additional setup after loading the view, typically from a nib. } - (void) sliderDidChange: (id)sender{ if ([sender isKindOfClass:[UISlider class]]){ UISlider *slider = sender; myLabel.text = [NSString stringWithFormat:@"%.2f",slider.value]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#import <UIKit/UIKit.h> @interface ViewController : UIViewController //就是那个 myLabel 罗,因为有个函数也调用了这个变量所以就写出来了 @property (strong, nonatomic) IBOutlet UILabel *myLabel; @end
使用 UISlider 动态调整字体大小:
#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize mylabel; - (void)viewDidLoad { [super viewDidLoad]; mylabel = [[UILabel alloc]init]; mylabel.frame = self.view.bounds; mylabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; mylabel.text = @"Title!"; mylabel.font = [UIFont boldSystemFontOfSize:48]; mylabel.textAlignment = NSTextAlignmentCenter; [self.view addSubview:mylabel]; UISlider *mySlider = [[UISlider alloc]init]; mySlider.frame = CGRectMake(0, 0, 250, 50); mySlider.minimumValue = 0.0; mySlider.maximumValue = 1.0; mySlider.value = 0.5; mySlider.center = self.view.center; //载入 UISlider 左右侧的图片 UIImage *image1 = [UIImage imageNamed:@"+.png"]; UIImage *image2 = [UIImage imageNamed:@"-.png"]; //赋值到 UISlider 实例 mySlider.minimumValueImage = image2; mySlider.maximumValueImage = image1; [self.view addSubview:mySlider]; //设置slider 数值变动时触发的函数 [mySlider addTarget:self action:@selector(sliderDidChange:) forControlEvents:UIControlEventValueChanged]; // Do any additional setup after loading the view, typically from a nib. } //slider value 变化时触发的函数 - (void) sliderDidChange:(id)sender{ if ([sender isKindOfClass:[UISlider class]]){ UISlider *slider = sender; mylabel.font = [UIFont boldSystemFontOfSize:96*slider.value]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#import <UIKit/UIKit.h> @interface ViewController : UIViewController //又是一个 UILabel 罗... @property (strong,nonatomic) UILabel *mylabel; @end
调用 UICircularSlider:
#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize mySlider,myCircularSlider,myProgress; - (void)viewDidLoad { [super viewDidLoad]; [self.myCircularSlider addTarget:self action:@selector(updateProgress:) forControlEvents:UIControlEventValueChanged]; [self.myCircularSlider setMinimumValue:self.mySlider.minimumValue]; [self.myCircularSlider setMinimumValue:self.mySlider.maximumValue]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)updateProgress:(UISlider *)sender { float progress = translateValueFromSourceIntervalToDestinationInterval(sender.value, sender.minimumValue, sender.maximumValue, 0.0, 10); [self.myProgress setProgress:progress]; [self.myCircularSlider setValue:progress]; [self.mySlider setValue:progress]; } @end
不知道哪里出了问题,报错耶,不管先23333