iOS 学习笔记三【segmentedControl分段控制器详细使用方法】

 

  在iOS开发过程中,分段控制器的使用频率还是蛮高的,下面是我写的一个简单的demo,大家可以把代码直接复制过去,就可以使用,ios9最新支持。

 

 

//
//  ViewController.m
//  03_Segmentedcontrol_Test
//
//  Created by 博爱之家 on 15/12/1.
//  Copyright © 2015年 博爱之家. All rights reserved.
//

#import "ViewController.h"

//宏定义
//当前设备的屏幕宽度
#define KSCREEN_WIDTH   [[UIScreen mainScreen] bounds].size.width

//当前设备的屏幕高度
#define KSCREEN_HEIGHT   [[UIScreen mainScreen] bounds].size.height

// 颜色
#define COLOR_C(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]

@interface ViewController ()

@property (nonatomic, strong) UIView *subView;
@property (nonatomic, strong) UIView *currentView;
@property (nonatomic, strong) UISegmentedControl *segmentedControl;
//滚动的下划线
@property (nonatomic,strong)UIView *lineView;

@property (nonatomic, assign) BOOL isView1;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 创建子视图
    CGRect frame = CGRectMake(0, 20 + 44, KSCREEN_WIDTH, KSCREEN_HEIGHT - 44);
    _subView = [[UIView alloc] initWithFrame:frame];
    [self.view addSubview:_subView];
    
    // 设置分段控制器
    [self setUpSegmentedControl];
    
    _isView1 = YES;
    [self enterView1];
    
    // 添加下划线
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 20+44, KSCREEN_WIDTH/3, 3)];
    self.lineView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_lineView];
}

#pragma mark 设置分段控制器
- (void)setUpSegmentedControl
{
    self.segmentedControl = [[UISegmentedControl alloc ]initWithItems:@[@"标题1",@"标题2",@"标题3"]];
    self.segmentedControl.frame = CGRectMake(0, 20, KSCREEN_WIDTH, 44);
    [self.view addSubview:self.segmentedControl];
    
    //设置控件的颜色
    self.segmentedControl.tintColor = COLOR_C(239, 239, 239, 1.0);
    
    //默认选中的索引
    self.segmentedControl.selectedSegmentIndex = 0;
    
    // 字体颜色
    NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,[UIFont fontWithName:@"AppleGothic"size:14],NSFontAttributeName ,nil];
    NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor],NSForegroundColorAttributeName,[UIFont fontWithName:@"AppleGothic"size:14],NSFontAttributeName ,nil];
    
    [self.segmentedControl setTitleTextAttributes:dict1 forState:UIControlStateNormal];
    [self.segmentedControl setTitleTextAttributes:dict2 forState:UIControlStateSelected];
    
    //设置在点击后是否恢复原样
    _segmentedControl.momentary = NO;
    
    // 点击事件
    [_segmentedControl addTarget:self action:@selector(controllerPressed:) forControlEvents:UIControlEventValueChanged];
}

#pragma mark - 点击事件
- (IBAction)controllerPressed:(UISegmentedControl *)sender
{
    self.lineView.hidden = NO;
    
    switch ([sender selectedSegmentIndex])
    {
        case 0:
        {
            CGRect lineFrame = self.lineView.frame;
            lineFrame.origin.x = 0;
            
            [UIView animateWithDuration:0.2 animations:^{
                self.lineView.frame = lineFrame;
            }];
            [self enterView1];
        }
            break;
        case 1:
        {
            CGRect frame = self.lineView.frame;
            frame.origin.x = KSCREEN_WIDTH/3;
            
            [UIView animateWithDuration:0.2 animations:^{
                
                self.lineView.frame = frame;
            }];
            
            [self enterView2];
        }
            break;
        case 2:
        {
            //给下划线 view 添加动画
            CGRect lineFrame = self.lineView.frame;
            lineFrame.origin.x = KSCREEN_WIDTH/3 * 2;
            
            [UIView animateWithDuration:0.2 animations:^{
                
                self.lineView.frame = lineFrame;
            }];
            [self enterView3];
        }
            break;
            
        default:
            break;
    }
    NSLog(@"Segment %ld selected\n", (long)sender.selectedSegmentIndex);
}

- (void)enterView1
{
    _isView1 = YES;
    UIView *view1 = [[UIView alloc] initWithFrame:self.subView.bounds];
    [self.currentView removeFromSuperview];
    view1.backgroundColor = [UIColor yellowColor];
    self.currentView = view1;
    [self.subView addSubview:view1];
}

- (void)enterView2
{
    _isView1 = NO;
    UIView *view2 = [[UIView alloc] initWithFrame:self.subView.bounds];
    [self.currentView removeFromSuperview];
    view2.backgroundColor = [UIColor blueColor];
    self.currentView = view2;
    [self.subView addSubview:view2];
}

- (void)enterView3
{
    _isView1 = NO;
    UIView *view3 = [[UIView alloc] initWithFrame:self.subView.bounds];
    [self.currentView removeFromSuperview];
    view3.backgroundColor = [UIColor greenColor];
    self.currentView = view3;
    [self.subView addSubview:view3];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

最后的效果如下:

 

 

 

posted @ 2015-12-01 21:04  博爱1616  阅读(...)  评论(...编辑  收藏