一 UIControl 是所有控制类控件的基类.
· 比如UIButton UISlider UISegmentedControl 等只要跟控件有关系的都继承于UIControl类
· 核心功能:
为控制控件通过addTarget: action:forControlEvents: 方法来添加事件
通过removeTarget:action: forControlEvents: 来一出事件
二 UISegmentedControl 是可以实现单选的控件
· UISegmentedControl 是iOS 中的分段控件
· 每一个segment都能被点击 相当于集成了若干个Button
· 通常我们会点击不同的segment来切换不同的View
#define kW [UIScreen mainScreen].bounds.size.width
@implementation
RootViewController
- (void)dealloc
{
[ _firstVC release ];
[ _secondtVC release ];
[ _thirdVC release ];
[ super dealloc ];
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
// Do any additional setup after loading the view.
// 先把三个控制器的 view 添加上
[ self addControllerView ];
// 再添加 segmentsdView
[ self addImageSegmented ];
[ _firstVC release ];
[ _secondtVC release ];
[ _thirdVC release ];
[ super dealloc ];
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
// Do any additional setup after loading the view.
// 先把三个控制器的 view 添加上
[ self addControllerView ];
// 再添加 segmentsdView
[ self addImageSegmented ];
}
//
创建
- (
void
)addSubViews
{
// 创建显示用的标题数组
NSArray *titleArray = @[ @" 第一 " , @" 第二 " , @" 第三 " ] ;
// 用数组初始化 分段按钮
UISegmentedControl *segmentControl = [[ UISegmentedControl alloc ] initWithItems :titleArray];
// 设置一下背景颜色
segmentControl. backgroundColor = [ UIColor colorWithRed : 0.440 green : 0.905 blue : 1.000 alpha : 1.000 ];
segmentControl. frame = CGRectMake (( kW - 300 ) / 2 , 30 , 300 , 40 );
// 选中时候默认是蓝色
segmentControl. tintColor = [ UIColor colorWithRed : 0.256 green : 0.552 blue : 1.000 alpha : 1.000 ];
// segmentControl.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic;
// 设置索引处宽
[segmentControl setWidth : 120 forSegmentAtIndex : 1 ];
// 重要属性 设置选中的索引 默认是没有选中的
// segmentControl.selectedSegmentIndex = 0;
[ self . view addSubview :segmentControl];
}
// 创建三个控制器出来 并且把控制器的 view 添加到 self.view 上面
- ( void )addControllerView
{
// 创建控制器
self . thirdVC = [[ ThirdViewController alloc ] init ];
[ self . view addSubview : self . thirdVC . view ];
[ _thirdVC release ];
self . secondtVC = [[ SecondViewController alloc ] init ];
[ self . view addSubview : self . secondtVC . view ];
[ _secondtVC release ];
self . firstVC = [[ FirstViewController alloc ] init ];
[ self . view addSubview : self . firstVC . view ];
[ _firstVC release ];
[ self . view bringSubviewToFront : self . firstVC . view ];
}
// 创建带图片的 UISegmentedControl
- ( void )addImageSegmented
{
// 创建显示用的标题数组
NSArray *titleArray = @[ @" 第一 " , @" 第二 " , @" 第三 " ] ;
// 用数组初始化 分段按钮
UISegmentedControl *segmentControl = [[ UISegmentedControl alloc ] initWithItems :titleArray];
// 设置一下背景颜色
segmentControl. backgroundColor = [ UIColor colorWithRed : 0.440 green : 0.905 blue : 1.000 alpha : 1.000 ];
segmentControl. frame = CGRectMake (( kW - 300 ) / 2 , 30 , 300 , 40 );
// 选中时候默认是蓝色
segmentControl. tintColor = [ UIColor colorWithRed : 0.256 green : 0.552 blue : 1.000 alpha : 1.000 ];
// segmentControl.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic;
// 设置索引处宽
[segmentControl setWidth : 120 forSegmentAtIndex : 1 ];
// 重要属性 设置选中的索引 默认是没有选中的
// segmentControl.selectedSegmentIndex = 0;
[ self . view addSubview :segmentControl];
}
// 创建三个控制器出来 并且把控制器的 view 添加到 self.view 上面
- ( void )addControllerView
{
// 创建控制器
self . thirdVC = [[ ThirdViewController alloc ] init ];
[ self . view addSubview : self . thirdVC . view ];
[ _thirdVC release ];
self . secondtVC = [[ SecondViewController alloc ] init ];
[ self . view addSubview : self . secondtVC . view ];
[ _secondtVC release ];
self . firstVC = [[ FirstViewController alloc ] init ];
[ self . view addSubview : self . firstVC . view ];
[ _firstVC release ];
[ self . view bringSubviewToFront : self . firstVC . view ];
}
// 创建带图片的 UISegmentedControl
- ( void )addImageSegmented
{
// 如果使用图片必须使用镂空图
UIImage
*image1 = [
UIImage
imageNamed
:
@"20-gear2"
];
UIImage *image2 = [ UIImage imageNamed : @"34-coffee" ];
UIImage *image3 = [ UIImage imageNamed : @"53-house" ];
// UIImage *image4 = [UIImage imageNamed:@"O9Z~Z)AT~}K1~(()096SPUC.jpg"];
NSArray *imageArray = @[ image1, image3, image2 ] ;
UIImage *image2 = [ UIImage imageNamed : @"34-coffee" ];
UIImage *image3 = [ UIImage imageNamed : @"53-house" ];
// UIImage *image4 = [UIImage imageNamed:@"O9Z~Z)AT~}K1~(()096SPUC.jpg"];
NSArray *imageArray = @[ image1, image3, image2 ] ;
UISegmentedControl *segmentControl1 = [[UISegmentedControl alloc]initWithItems:imageArray];
segmentControl1.frame = CGRectMake((kW - 300) / 2, 30, 300, 40);
segmentControl1.
backgroundColor
= [
UIColor
colorWithRed
:
0.963
green
:
1.000
blue
:
0.940
alpha
:
1.000
];
segmentControl1. tintColor = [ UIColor colorWithRed : 1.000 green : 0.447 blue : 0.098 alpha : 1.000 ];
// 最重要的方法 添加点击方法
// 这个方法是其父类 UIControl 的
segmentControl1. selectedSegmentIndex = 0 ;
[segmentControl1 addTarget : self action : @selector (actionSegmented:) forControlEvents :( UIControlEventValueChanged )]; // 此处的事件是通过数组的索引变化来知道点按的哪一个分段按钮 使用 UIControlEventValueChanged 来判断的
[ self . view addSubview :segmentControl1];
[segmentControl1 release ];
segmentControl1. tintColor = [ UIColor colorWithRed : 1.000 green : 0.447 blue : 0.098 alpha : 1.000 ];
// 最重要的方法 添加点击方法
// 这个方法是其父类 UIControl 的
segmentControl1. selectedSegmentIndex = 0 ;
[segmentControl1 addTarget : self action : @selector (actionSegmented:) forControlEvents :( UIControlEventValueChanged )]; // 此处的事件是通过数组的索引变化来知道点按的哪一个分段按钮 使用 UIControlEventValueChanged 来判断的
[ self . view addSubview :segmentControl1];
[segmentControl1 release ];
}
//
用
segmented
实现界面之间的切换
/**
* 1. 创建三个控制器 为了测试 加上背景
2. 点哪个 就把哪个放到最前面来
3. 显示的是啥 就是某个控制器的 self.view
4. 之前我们可以实现父视图中通过角标的视图罗列顺序将某个需要的控制器的视图方法到最前面来
5. 因为放到最前面会挡到 segmented 所以再将 segmented 放到最前面
/**
* 1. 创建三个控制器 为了测试 加上背景
2. 点哪个 就把哪个放到最前面来
3. 显示的是啥 就是某个控制器的 self.view
4. 之前我们可以实现父视图中通过角标的视图罗列顺序将某个需要的控制器的视图方法到最前面来
5. 因为放到最前面会挡到 segmented 所以再将 segmented 放到最前面
*/
- (void)actionSegmented:(UISegmentedControl *)segment
{
NSLog ( @"%ld" , segment. selectedSegmentIndex );
switch (segment. selectedSegmentIndex ) {
case 0 :
[ self . view bringSubviewToFront : self . firstVC . view ];
// [self.view bringSubviewToFront:segment];
NSLog ( @" 第一界面 " );
break ;
case 1 :
[ self . view bringSubviewToFront : self . secondtVC . view ];
// [self.view bringSubviewToFront:segment];
NSLog ( @" 第二界面 " );
break ;
case 2 :
[ self . view bringSubviewToFront : self . thirdVC . view ];
// [self.view bringSubviewToFront:segment];
NSLog ( @" 第三界面 " );
break ;
default :
break ;
}
// 每次都要把 segment 放到最上面
[ self . view bringSubviewToFront :segment];
// if (segment.selectedSegmentIndex == 0) {
// NSLog(@" 第一界面 ");
// } else if (segment.selectedSegmentIndex == 1) {
// NSLog(@" 第二界面 ");
// } else if (segment.selectedSegmentIndex == 2) {
// NSLog(@" 第三界面 ");
// }
NSLog ( @"%ld" , segment. selectedSegmentIndex );
switch (segment. selectedSegmentIndex ) {
case 0 :
[ self . view bringSubviewToFront : self . firstVC . view ];
// [self.view bringSubviewToFront:segment];
NSLog ( @" 第一界面 " );
break ;
case 1 :
[ self . view bringSubviewToFront : self . secondtVC . view ];
// [self.view bringSubviewToFront:segment];
NSLog ( @" 第二界面 " );
break ;
case 2 :
[ self . view bringSubviewToFront : self . thirdVC . view ];
// [self.view bringSubviewToFront:segment];
NSLog ( @" 第三界面 " );
break ;
default :
break ;
}
// 每次都要把 segment 放到最上面
[ self . view bringSubviewToFront :segment];
// if (segment.selectedSegmentIndex == 0) {
// NSLog(@" 第一界面 ");
// } else if (segment.selectedSegmentIndex == 1) {
// NSLog(@" 第二界面 ");
// } else if (segment.selectedSegmentIndex == 2) {
// NSLog(@" 第三界面 ");
// }
}
三 UIImageView 除了能显示单张图片,还能处理一组图片的播放
- (
void
)dealloc
{
[ _imageView release ];
[ _startButton release ];
[ _stopButton release ];
[ super dealloc ];
}
- ( instancetype )initWithFrame:( CGRect )frame
{
self = [ super initWithFrame :frame];
if ( self ) {
[ self addSubViews ];
}
return self ;
}
// 视图的布局方法
- ( void )addSubViews
{
// 创建 imageView
self . imageView = [[ UIImageView alloc ] initWithFrame : CGRectMake (( kScreenWidth - 300 ) / 2 , 100 , 300 , 300 )];
self . imageView . backgroundColor = [ UIColor redColor ];
[ self addSubview : self . imageView ];
[ _imageView release ];
// 创建开始 Button
self . startButton = [ UIButton buttonWithType :( UIButtonTypeCustom )];
self . startButton . frame = CGRectMake ( self . imageView . left , self . imageView . bottom + 20 , 60 , 30 );
[ self . startButton setTitle : @" 开始 " forState :( UIControlStateNormal )];
[ self . startButton setTitle : @" 开始 " forState :( UIControlStateHighlighted )];
[ self . startButton setTitleColor :[ UIColor grayColor ] forState :( UIControlStateHighlighted )];
self . startButton . backgroundColor = [ UIColor blueColor ];
[ self addSubview : self . startButton ];
[ _startButton release ];
// 创建结束按钮
self . stopButton = [ UIButton buttonWithType :( UIButtonTypeCustom )];
self . stopButton . frame = CGRectMake ( self . imageView . right - 60 , self . imageView . bottom + 20 , 60 , 30 );
self . stopButton . backgroundColor = [ UIColor blueColor ];
[ self . stopButton setTitle : @" 结束 " forState :( UIControlStateNormal )];
[ self . stopButton setTitle : @" 结束 " forState :( UIControlStateHighlighted )];
[ self . stopButton setTitleColor :[ UIColor grayColor ] forState :( UIControlStateHighlighted )];
[ self addSubview : self . stopButton ];
{
[ _imageView release ];
[ _startButton release ];
[ _stopButton release ];
[ super dealloc ];
}
- ( instancetype )initWithFrame:( CGRect )frame
{
self = [ super initWithFrame :frame];
if ( self ) {
[ self addSubViews ];
}
return self ;
}
// 视图的布局方法
- ( void )addSubViews
{
// 创建 imageView
self . imageView = [[ UIImageView alloc ] initWithFrame : CGRectMake (( kScreenWidth - 300 ) / 2 , 100 , 300 , 300 )];
self . imageView . backgroundColor = [ UIColor redColor ];
[ self addSubview : self . imageView ];
[ _imageView release ];
// 创建开始 Button
self . startButton = [ UIButton buttonWithType :( UIButtonTypeCustom )];
self . startButton . frame = CGRectMake ( self . imageView . left , self . imageView . bottom + 20 , 60 , 30 );
[ self . startButton setTitle : @" 开始 " forState :( UIControlStateNormal )];
[ self . startButton setTitle : @" 开始 " forState :( UIControlStateHighlighted )];
[ self . startButton setTitleColor :[ UIColor grayColor ] forState :( UIControlStateHighlighted )];
self . startButton . backgroundColor = [ UIColor blueColor ];
[ self addSubview : self . startButton ];
[ _startButton release ];
// 创建结束按钮
self . stopButton = [ UIButton buttonWithType :( UIButtonTypeCustom )];
self . stopButton . frame = CGRectMake ( self . imageView . right - 60 , self . imageView . bottom + 20 , 60 , 30 );
self . stopButton . backgroundColor = [ UIColor blueColor ];
[ self . stopButton setTitle : @" 结束 " forState :( UIControlStateNormal )];
[ self . stopButton setTitle : @" 结束 " forState :( UIControlStateHighlighted )];
[ self . stopButton setTitleColor :[ UIColor grayColor ] forState :( UIControlStateHighlighted )];
[ self addSubview : self . stopButton ];
[_stopButton release];
}
@end
RootViewControl 里面
- (
void
)viewDidLoad {
[ super viewDidLoad ];
// Do any additional setup after loading the view.
ShuangJieView *sView = [[ ShuangJieView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ];
sView. backgroundColor = [ UIColor grayColor ];
[ self . view addSubview :sView];
[sView release ];
NSMutableArray *imageArray = [ NSMutableArray array ];
for ( int i = 1 ; i < 6 ; i++) {
NSString *imageName = [ NSString stringWithFormat : @"%d" , i];
NSLog ( @"%@" , imageName);
// 创建图片添加到数组里
UIImage *image = [ UIImage imageNamed :imageName];
[imageArray addObject :image];
}
// 给 imageView 设置属性
sView. imageView . animationImages = imageArray;
sView. imageView . animationRepeatCount = 0 ;
sView. imageView . animationDuration = 0.1 ;
// [sView.imageView startAnimating];
[sView. startButton addTarget : self action : @selector (actionStartButton:) forControlEvents :( UIControlEventTouchUpInside )];
[sView. stopButton addTarget : self action : @selector (actionStopButton:) forControlEvents :( UIControlEventTouchUpInside )];
[ super viewDidLoad ];
// Do any additional setup after loading the view.
ShuangJieView *sView = [[ ShuangJieView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ];
sView. backgroundColor = [ UIColor grayColor ];
[ self . view addSubview :sView];
[sView release ];
NSMutableArray *imageArray = [ NSMutableArray array ];
for ( int i = 1 ; i < 6 ; i++) {
NSString *imageName = [ NSString stringWithFormat : @"%d" , i];
NSLog ( @"%@" , imageName);
// 创建图片添加到数组里
UIImage *image = [ UIImage imageNamed :imageName];
[imageArray addObject :image];
}
// 给 imageView 设置属性
sView. imageView . animationImages = imageArray;
sView. imageView . animationRepeatCount = 0 ;
sView. imageView . animationDuration = 0.1 ;
// [sView.imageView startAnimating];
[sView. startButton addTarget : self action : @selector (actionStartButton:) forControlEvents :( UIControlEventTouchUpInside )];
[sView. stopButton addTarget : self action : @selector (actionStopButton:) forControlEvents :( UIControlEventTouchUpInside )];
}
- (
void
)actionStartButton:(
UIButton
*)button
{
// 接受到view 值 除了tag 设置属性 还可以用以下的方法
UIView
*superView = button.
superview
;
NSArray *subViews = superView. subviews ;
for ( UIView *view in subViews) {
if ([view isKindOfClass :[ UIImageView class ]]) {
UIImageView *imageView = ( UIImageView *)view;
[imageView startAnimating ];
}
}
}
- ( void )actionStopButton:( UIButton *)button
{
UIView *superView = button. superview ;
NSArray *subViews = superView. subviews ;
for ( UIView *view in subViews) {
if ([view isKindOfClass :[ UIImageView class ]]) {
UIImageView *imageView = ( UIImageView *)view;
[imageView stopAnimating ];
}
}
NSArray *subViews = superView. subviews ;
for ( UIView *view in subViews) {
if ([view isKindOfClass :[ UIImageView class ]]) {
UIImageView *imageView = ( UIImageView *)view;
[imageView startAnimating ];
}
}
}
- ( void )actionStopButton:( UIButton *)button
{
UIView *superView = button. superview ;
NSArray *subViews = superView. subviews ;
for ( UIView *view in subViews) {
if ([view isKindOfClass :[ UIImageView class ]]) {
UIImageView *imageView = ( UIImageView *)view;
[imageView stopAnimating ];
}
}
}
四 UISlider 是可以响应滑动事件的控件
- (
void
)addSlider{
UISlider *slider = [[ UISlider alloc ] initWithFrame : CGRectMake ( 30 , 100 , 300 , 40 )];
slider. backgroundColor = [ UIColor whiteColor ];
// 设置划过去的线的颜色
slider. minimumTrackTintColor = [ UIColor redColor ];
// 设置未划过线的颜色
slider. maximumTrackTintColor = [ UIColor greenColor ];
// 给中间球设置颜色
slider. thumbTintColor = [ UIColor orangeColor ];
// 设置图片
// 设置滑动球的图片
[slider setThumbImage :[ UIImage imageNamed : @"29-heart" ] forState :( UIControlStateNormal )];
[slider setThumbImage :[ UIImage imageNamed : @"77-ekg" ] forState :( UIControlStateHighlighted )];
[slider setThumbImage :[ UIImage imageNamed : @"Selected" ] forState :( UIControlStateSelected )];
//slider.selected = YES;
slider. minimumValueImage = [ UIImage imageNamed : @"07-map-marker" ];
UISlider *slider = [[ UISlider alloc ] initWithFrame : CGRectMake ( 30 , 100 , 300 , 40 )];
slider. backgroundColor = [ UIColor whiteColor ];
// 设置划过去的线的颜色
slider. minimumTrackTintColor = [ UIColor redColor ];
// 设置未划过线的颜色
slider. maximumTrackTintColor = [ UIColor greenColor ];
// 给中间球设置颜色
slider. thumbTintColor = [ UIColor orangeColor ];
// 设置图片
// 设置滑动球的图片
[slider setThumbImage :[ UIImage imageNamed : @"29-heart" ] forState :( UIControlStateNormal )];
[slider setThumbImage :[ UIImage imageNamed : @"77-ekg" ] forState :( UIControlStateHighlighted )];
[slider setThumbImage :[ UIImage imageNamed : @"Selected" ] forState :( UIControlStateSelected )];
//slider.selected = YES;
slider. minimumValueImage = [ UIImage imageNamed : @"07-map-marker" ];
slider.maximumValueImage = [UIImage imageNamed:@"53-house"];
// 重要属性
[slider
setMinimumValue
:
0
];
[slider setMaximumValue : 100.0 ];
slider. value = 50.0 ;
[slider setMaximumValue : 100.0 ];
slider. value = 50.0 ;
[slider addTarget:self action:@selector(actionslider:) forControlEvents:(UIControlEventValueChanged)];
[ self . view addSubview :slider];
[slider release ];
}
- ( void )actionslider:( UISlider *)slider
{
UILabel *label = [[ UILabel alloc ] initWithFrame : CGRectMake ( 100 , 400 , 300 , 60 )];
label. backgroundColor = [ UIColor whiteColor ];
label. text = [ NSString stringWithFormat : @"%f" , slider. value ];
label. textColor = [ UIColor redColor ];
[ self . view addSubview :label];
[label release ];
}