UISlider的简单用法

这次只上一个demo,一看便知。

#import "XCViewController.h"

@interface XCViewController ()

@property (nonatomic, strong)UISlider * mySlider;

@property (nonatomic, strong)UILabel * myLabel;

@end

@implementation XCViewController

- (void)viewDidLoad

{

[super viewDidLoad];

// 创建一个UISlider

_mySlider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 200, 23)];

// 定位UISlider的位置

_mySlider.center = self.view.center;

// 设置UISlider的最小值和最大值

_mySlider.minimumValue = 2;

_mySlider.maximumValue = 50;

// 设置UISlider的初始值

_mySlider.value = 12;

// 为UISlider添加事件方法

[_mySlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];

// 定义UISlider的样式

_mySlider.minimumTrackTintColor = [UIColor redColor];

_mySlider.maximumTrackTintColor = [UIColor greenColor];

_mySlider.thumbTintColor = [UIColor blueColor];

// 新建一个UILabel对象

_myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 320, 50)];

// 设置这个UILabel对象的字体为系统字体,大小为当前UISlider的值

_myLabel.font = [UIFont systemFontOfSize:_mySlider.value];

_myLabel.text = @"XCoder Studio";

// 将UISlider添加到一个UIView视图中

[self.view addSubview:_mySlider];

// 将UILabel添加到一个UIView视图中

[self.view addSubview:_myLabel];

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (void)sliderValueChanged:(id)sender {

if([sender isKindOfClass:[UISliderclass]]) {

UISlider * slider = (UISlider *)sender;

_myLabel.font = [UIFont systemFontOfSize:slider.value];

}

}

@end

你可能感兴趣的:(UISlider的简单用法)