码迷,mamicode.com
首页 > 其他好文 > 详细

082自定义一个滑块

时间:2015-06-15 22:05:05      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

效果如下:

技术分享

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

 

082自定义一个滑块

标签:

原文地址:http://www.cnblogs.com/huangjianwu/p/4579196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!