UIControl的子类UISwitch, UISegmentedCntrol, UIPageControl详解

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UISwitch *swit = [[UISwitch alloc] initWithFrame:CGRectMake(20, 300, 100, 20)];
    [self.view addSubview:swit];
    // 3. (开关按钮)圆圈的颜色
    swit.thumbTintColor = [UIColor yellowColor];
    // switch 宽和高不影响控件, 只有x和y
    // 1. 开启时的控件内部颜色
    swit.onTintColor = [UIColor blueColor];
    // 2. 边框及切换到关闭的时的颜色(non_resettable)
    swit.tintColor = nil;
    // 4. 打开时的图片, ios7后失效
    swit.onImage = [UIImage imageNamed:@"456"];
    // 5. 关闭时的图片, 也失效
    swit.offImage = [UIImage imageNamed:@"456"];
    // 6. 判断switchControl的开闭状态
    // 通过setter方法, 给变switchControl的初始状态
    swit.on = YES;
    NSLog(@"%@", swit.isOn ? @"yes" : @"no");
    [swit setOn:YES animated:NO];
    // 添加绑定事件(与button相同)
    [swit addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
   
    
    // slider
    // 高度不影响控件的高度, 这个控件永远居中与视图中间
    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 100, 100, 20)];
    [self.view addSubview:slider];
   // slider.minimumValueImage = [UIImage imageNamed:@"456"];
    // 1. 滑块最大值和最小值, 最大和最小可以根据需求去改变
    slider.maximumValue = 200.0f;
    // 2. 当前滑块的值
    slider.value = 50.0f;
    [slider setValue:100 animated:YES];
    // 3. 最小值和最大值显示的图片
    //  maximumValueImage  minimumValueImage
    // 4. containous 为no时, 只在按下和松手是触发方法
    slider.continuous = NO;
    NSLog(@"%@", slider.isContinuous ? @"yes" : @"no");
    // 5. 绑定事件
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
   
    CGRect rect = [slider maximumValueImageRectForBounds:slider.bounds];
    NSLog(@"%f, %f", rect.size.width, rect.size.height);
    // 8. maximumTrackTintColor 未移动到的区域的颜色
    slider.maximumTrackTintColor = [UIColor blueColor];
    // 9. minimumTrackTintColor 已移动的区域的颜色
    slider.minimumTrackTintColor = [UIColor whiteColor];
    // 10. 滑块的颜色
    // slider.thumbTintColor = [UIColor blueColor];
    // 11. 设置进度条滑块左边的图片
    [slider setMinimumTrackImage:[UIImage imageNamed:@"player_slider_playback_right"] forState:UIControlStateNormal];
    // 12. 设置进度条滑块右边的图片
    [slider setMaximumTrackImage:[UIImage imageNamed:@"player_slider_playback_left"] forState:UIControlStateNormal];
    // 13. 设置圆圈的图片
    [slider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"] forState:UIControlStateNormal];
    
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"first", @"second", @"last", @"one", @"two"]];
    // segment不给frame, 系统会自动计算大小, 推荐, 自己加上frame
    self.seg.frame = CGRectMake(0, 0, 600, 50);
    [self.view addSubview:self.seg];
    // 1. momentary 设置为yes 如果点击选中没有选中的效果,
    // 默认为NO
    self.seg.momentary = YES;
    // 2. seg的控制段的数量
    NSInteger number = self.seg.numberOfSegments;
    NSLog(@"分段控制器的段数:(items的个数) %ld", number);
    // 3. remove 移除
    // 4. insertSegmentWithTitle:@"name" atIndex:2 animated:YES 插入
    // 5. setTitle:forSegmentAtIndex: 用来修改对应下标的标题
    // 6. setWidth:0 forSegmentAtIndex: 默认是0
    [self.seg setWidth:0 forSegmentAtIndex:0];
    // 7. setContentOffset:<#(CGSize)#> forSegmentAtIndex:<#(NSUInteger)#> 设置对应下标内容的偏移
    [self.seg setContentOffset:CGSizeMake(20, 0) forSegmentAtIndex:0];
    // 8. 能否被选中
    [self.seg setEnabled:NO forSegmentAtIndex:0];
    // 9. selectedSegmentIndex 开始选中的下标
    self.seg.selectedSegmentIndex = 1;
    // 10. tintColor文字及边框颜色
    self.seg.tintColor = [UIColor greenColor];
    // 11. apportionsSegmentWidthsByContent 默认为no
    // 如果分段控制器的段宽为0时, 并且该属性为YES, 系统会自动计算
    self.seg.apportionsSegmentWidthsByContent = YES;
    // 12. 绑定事件
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    
    UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(10, 500, 100, 30)];
    page.backgroundColor = [UIColor blackColor];
    // 1. 分页控制器的页数 默认为 0
    page.numberOfPages = 5;
    // 2. 当前页
    page.currentPage = 3;
    // 3. 当只有一页的时候pageControl隐藏
    page.hidesForSinglePage = YES;
    // 4.
    CGSize size = [page sizeForNumberOfPages:4];
    NSLog(@"size++%@", NSStringFromCGSize(size));
    // 其他页的颜色
    page.pageIndicatorTintColor = [UIColor redColor];
    // 当前页的颜色
    page.currentPageIndicatorTintColor = [UIColor whiteColor];
    [self.view addSubview:page];
    // 绑定事件
    [page addTarget:self action:@selector(pageControllChange:) forControlEvents:UIControlEventValueChanged];
    // 设置为yes, 关闭系统事件page的切换, 直到调用updateCurrentPageDisplay的方法, 才会显示
    page.defersCurrentPageDisplay = YES;
}
- (void)pageControllChange:(UIPageControl *)page {
    NSLog(@"Change");
    [page updateCurrentPageDisplay];
}
- (void)segAction:(UISegmentedControl *)seg {
    NSLog(@"%ld", seg.selectedSegmentIndex);
    switch (seg.selectedSegmentIndex) {
        case 1:
            
            break;
        case 2:
            
            break;
            // ....
        default:
            break;
    }
}
    //
- (void)sliderAction:(UISlider *)slider {
    NSLog(@"%f", slider.value);
    
}
- (void)switchAction:(UISwitch *)swit {
        NSLog(@"开关事件");
    [self.seg insertSegmentWithTitle:@"name" atIndex:2 animated:YES];
    [self.seg insertSegmentWithImage:[UIImage imageNamed:@"player_slider_playback_thumb"] atIndex:2 animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 

posted @ 2016-07-19 17:51  ma_fighting  阅读(295)  评论(0编辑  收藏  举报
历史天气查