1 前言 UISlider控件允許用戶用一個簡單只管的界面來在一個范圍內選擇一個值。 2 代碼實例 ZYViewController.h: #import <UIKit/UIKit.h> @interface ZYViewController : UIViewController @property(nonatomic,strong) UISlider *mySlider; @end #import <UIKit/UIKit.h> @interface ZYViewController : UIViewController @property(nonatomic,strong) UISlider *mySlider; @end ZYViewController.m: @synthesize mySlider; - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.mySlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 23.0f)]; mySlider.center = self.view.center; self.mySlider.minimumValue = 0.0f;//滑動條的最小值 self.mySlider.maximumValue = 100.0f;//滑動條的最大值 self.mySlider.value = mySlider.maximumValue/2.0;//滑動條的當前值 [mySlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];//添加滑動事件 mySlider.continuous = NO;//設置只有在離開滑動條的最後時刻才觸發滑動事件 [mySlider setThumbImage:[UIImage imageNamed:@"slider.png"] forState:UIControlStateHighlighted];//設置滑動的時候顯示的圖片 [self.view addSubview:mySlider];//添加視圖 } //添加滑動事件 -(void)sliderValueChanged:(UISlider *)paramSender{ if ([paramSender isEqual:self.mySlider]) { NSLog(@"New value=%f",paramSender.value); } } @synthesize mySlider; - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.mySlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 23.0f)]; mySlider.center = self.view.center; self.mySlider.minimumValue = 0.0f;//滑動條的最小值 self.mySlider.maximumValue = 100.0f;//滑動條的最大值 self.mySlider.value = mySlider.maximumValue/2.0;//滑動條的當前值 [mySlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];//添加滑動事件 mySlider.continuous = NO;//設置只有在離開滑動條的最後時刻才觸發滑動事件 [mySlider setThumbImage:[UIImage imageNamed:@"slider.png"] forState:UIControlStateHighlighted];//設置滑動的時候顯示的圖片 [self.view addSubview:mySlider];//添加視圖 } //添加滑動事件 -(void)sliderValueChanged:(UISlider *)paramSender{ if ([paramSender isEqual:self.mySlider]) { NSLog(@"New value=%f",paramSender.value); } }
運行結果:
初始狀態:
滑動時候: