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

UISlider显示进度

时间:2016-04-10 01:14:33      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

图片展示效果如下:

技术分享

其他没什么好说的,直接上代码:

RootView.h:

1 #import <UIKit/UIKit.h>
2 
3 @interface RootView : UIView
4 @property (nonatomic, strong) UISlider *slider;
5 @property (nonatomic, strong) UILabel *label;
6 
7 @end

RootView.m:

 1 #import "RootView.h"
 2 
 3 @implementation RootView
 4 
 5 - (instancetype)initWithFrame:(CGRect)frame {
 6     if (self = [super initWithFrame:frame]) {
 7         // 添加子视图
 8         [self add];
 9     }
10     return self;
11 }
12 
13 
14 // 添加子视图
15 - (void)add {
16     
17     // 添加slider
18     self.slider = [[UISlider alloc] initWithFrame:CGRectMake(50, 300, self.frame.size.width - 50 * 2, 50)];
19     self.slider.minimumValue = 0.0;
20     self.slider.maximumValue = 100.0;
21     [self addSubview:self.slider];
22     
23     // 添加label
24     self.label = [[UILabel alloc] init];
25     self.label.textAlignment = NSTextAlignmentCenter;
26     self.label.layer.cornerRadius = 15;
27     self.label.layer.borderWidth = 2;
28     self.label.layer.borderColor = [UIColor greenColor].CGColor;
29     self.label.font = [UIFont fontWithName:@"Gill Sans" size:12];
30     [self addSubview:self.label];
31     
32 }
33 
34 @end

RootViewController.m:

 1 #import "RootViewController.h"
 2 #import "RootView.h"
 3 
 4 @interface RootViewController ()
 5 @property (nonatomic, strong) RootView *rootView;
 6 
 7 @end
 8 
 9 @implementation RootViewController
10 
11 - (void)loadView {
12     self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
13     self.view = self.rootView;
14 }
15 
16 
17 - (void)viewDidLoad {
18     [super viewDidLoad];
19     
20     // 为slider添加一个滑动事件(注意添加的事件是改变数据的事件UIControlEventValueChanged)
21     [self.rootView.slider addTarget:self action:@selector(slding:) forControlEvents:UIControlEventValueChanged];
22 }
23 
24 // 实现滑动显示进度(百分比)
25 - (void)slding:(UISlider *)sender {
26     // 创建一个图片控制器,并且获取slider控件的某一个子控件(就是进度条的滑块,位置是2)
27     UIImageView *imageView = [self.rootView.slider.subviews objectAtIndex:2];
28     // 获取滑块在父视图上的位置
29     CGRect nowRect = [self.rootView convertRect:imageView.frame fromView:imageView.superview];
30     // 根据滑块的位置来设置label的位置
31     self.rootView.label.frame = CGRectMake(nowRect.origin.x - 10, nowRect.origin.y - 30, nowRect.size.width, nowRect.size.height);
32     // 定义一个NSInteger类型的变量来接收滑块当前滑动到的位置
33     NSInteger x = self.rootView.slider.value;
34     // 在label上显示滑块的位置
35     self.rootView.label.text = [NSString stringWithFormat:@"%ld%%",x];
36 }
37 
38 
39 - (void)didReceiveMemoryWarning {
40     [super didReceiveMemoryWarning];
41     // Dispose of any resources that can be recreated.
42 }
43 
44 @end

 

UISlider显示进度

标签:

原文地址:http://www.cnblogs.com/zhizunbao/p/5373011.html

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