IOS开发(14)之UISlider控件
1 前言
UISlider控件允许用户用一个简单只管的界面来在一个范围内选择一个值。
2 代码实例
ZYViewController.h:
[plain]
#import <UIKit/UIKit.h>
@inte易做图ce ZYViewController : UIViewController
@property(nonatomic,strong) UISlider *mySlider;
@end
#import <UIKit/UIKit.h>
@inte易做图ce ZYViewController : UIViewController
@property(nonatomic,strong) UISlider *mySlider;
@end
ZYViewController.m:
[plain]
@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);
}
}运行结果:
初始状态:
滑动时候:
补充:移动开发 , IOS ,