标签:
效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 - (void)sliderDidChange:(UISlider *)sender; 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 [self layoutUI]; 14 } 15 16 - (void)didReceiveMemoryWarning { 17 [super didReceiveMemoryWarning]; 18 // Dispose of any resources that can be recreated. 19 } 20 21 - (void)layoutUI { 22 CGFloat currentColorVal = 0.5; 23 self.view.backgroundColor = [UIColor colorWithRed:currentColorVal green:currentColorVal blue:currentColorVal alpha:1.0]; 24 25 UISlider *sldCustom = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 260, 50)]; 26 sldCustom.center = self.view.center; 27 sldCustom.minimumValue = 0.0; 28 sldCustom.maximumValue = 1.0; 29 sldCustom.value = currentColorVal; 30 31 //sldCustom.minimumTrackTintColor = [UIColor colorWithRed:0.961 green:1.000 blue:0.643 alpha:1.000]; 32 //sldCustom.maximumTrackTintColor = [UIColor colorWithRed:0.853 green:0.296 blue:1.000 alpha:1.000]; 33 //从最小值到滑块,杆的图片 34 UIImage *imgTrackLeft = [UIImage imageNamed:@"SliderTrackLeft"]; 35 imgTrackLeft = [imgTrackLeft stretchableImageWithLeftCapWidth:4 topCapHeight:0]; 36 [sldCustom setMinimumTrackImage:imgTrackLeft forState:UIControlStateNormal]; 37 //从最大值到滑块,杆的图片 38 UIImage *imgTrackRight = [UIImage imageNamed:@"SliderTrackRight"]; 39 imgTrackRight = [imgTrackRight stretchableImageWithLeftCapWidth:4 topCapHeight:0]; 40 [sldCustom setMaximumTrackImage:imgTrackRight forState:UIControlStateNormal]; 41 42 //设置滑块正常和高亮状态下的图片 43 [sldCustom setThumbImage:[UIImage imageNamed:@"SliderThumb"] forState:UIControlStateNormal]; 44 [sldCustom setThumbImage:[UIImage imageNamed:@"SliderThumbHighlighted"] forState:UIControlStateHighlighted]; 45 [sldCustom addTarget:self 46 action:@selector(sliderDidChange:) 47 forControlEvents:UIControlEventValueChanged]; 48 [self.view addSubview:sldCustom]; 49 } 50 51 - (void)sliderDidChange:(UISlider *)sender { 52 CGFloat currentColorVal = sender.value; 53 NSLog(@"currentColorVal=%0.2f", currentColorVal); //currentColorVal=0.50 54 self.view.backgroundColor = [UIColor colorWithRed:currentColorVal green:currentColorVal blue:currentColorVal alpha:1.0]; 55 } 56 57 @end
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4579196.html