2015-12-15- UI5_UIControl-
//
// RootViewController.m
// UI5_UIControl
//
// Created by YIem on 15/12/11.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//
import "RootViewController.h"
// UI枚举实现 练习 红黄蓝 + Switch 循环
typedef NS_ENUM(NSUInteger, hhl) {
RedColor,
YellColor,
BlueColor,
};
@interface RootViewController ()
@end
@implementation RootViewController
(void)viewDidLoad {
[super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor];
if 0
///******UIcontrol******////
/***< SegmentedConteol 分段控制器>*/
UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"王者", @"大师", @"钻石", @"白金", @"黄金", @"白银", @"青铜"]];
// 设置frame
seg.frame = CGRectMake(50, 100, 300, 50);
// 添加父视图
[self.view addSubview:seg];
[seg release];
// 背景颜色
seg.backgroundColor = [UIColor yellowColor];
// 边框颜色
seg.tintColor = [UIColor blackColor];
// 分段索引 // 初始默认选中为第几位
seg.selectedSegmentIndex = 2;
// 添加一个新的分段
[seg insertSegmentWithTitle:@"卞一" atIndex:0 animated:YES];
// 添加点击事件
// valueChange 值变化时 触发方法
[seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
endif
if 0
// 练习: 创建分段颜色,红黄蓝 点击改变self.view背景颜色
UISegmentedControl *hhl = [[UISegmentedControl alloc]initWithItems:@[@"红", @"黄", @"蓝"]];
hhl.frame = CGRectMake(100, 100, 100, 40);
[self.view addSubview:hhl];
[hhl release];
hhl.backgroundColor = [UIColor yellowColor];
hhl.tintColor = [UIColor blackColor];
//hhl.selectedSegmentIndex = 0;
[hhl addTarget:self action:@selector(hhlAction:) forControlEvents:UIControlEventValueChanged];
endif
if 0
/***** < slider 滑动控制器>*****/
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 300, 300, 50)];
slider.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:slider];
[slider release];
// 添加事件
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
// 最大值
slider.maximumValue = 100;
// 最小值
slider.minimumValue = -100;
// 设置起始点
// value控制滑快起始位置
slider.value = -100;
// 滑动过的颜色 左侧颜色
slider.minimumTrackTintColor = [UIColor redColor];
// 滑动过的颜色 右侧颜色
slider.maximumTrackTintColor = [UIColor blackColor];
// 滑快颜色
slider.thumbTintColor = [UIColor greenColor];
// 颜色也可以通过RGB计算
//[UIColor colorWithHue:<#(CGFloat)#> saturation:<#(CGFloat)#> brightness:<#(CGFloat)#> alpha:<#(CGFloat)#>]
endif
if 0
////**** switch 开关 ****////
UISwitch *kaiguan = [[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
kaiguan.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:kaiguan];
[kaiguan release];
// switch 大小 w:51 h:21
NSLog(@"%@", NSStringFromCGRect(kaiguan.frame));
// 开启 状态 的颜色
kaiguan.onTintColor = [UIColor magentaColor];
// 关闭 状态下的颜色
kaiguan.tintColor = [UIColor cyanColor];
// 按钮颜色
kaiguan.thumbTintColor = [UIColor blackColor];
// 添加事件
[kaiguan addTarget:self action:@selector(kaiguanAction:) forControlEvents:UIControlEventValueChanged];
endif
if 0
/**** pageControl 页码控制器 *****/
UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectMake(100, 100, 150, 40)];
page.backgroundColor = [UIColor blackColor];
[self.view addSubview:page];
[page release];
// 页数 保证控制器大小和页码数量一致
page.numberOfPages = 8;
// 设置当前页
page.currentPage = 1;
// 页码颜色
page.pageIndicatorTintColor = [UIColor redColor];
// 当前页面颜色
page.currentPageIndicatorTintColor = [UIColor greenColor];
// 添加事件
[page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
endif
if 1
/*** stepper 步进控制器*****/
UIStepper *stepper = [[UIStepper alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
stepper.backgroundColor = [UIColor redColor];
[self.view addSubview:stepper];
[stepper release];
// 最小值
stepper.minimumValue = 0;
// 最大值
stepper.maximumValue = 100;
// 每次变化大小
stepper.stepValue = 5;
// 当前起始
stepper.value = 3;
// 值变化是否都显示(响应)
stepper.continuous = YES;
// 安住时是否自动变化
stepper.autorepeat = YES;
// 是否允许从max回到min
stepper.wraps = YES;
[stepper addTarget:self action:@selector(stepperAction:) forControlEvents:UIControlEventValueChanged];
endif
}
pragma mark - 分段控制器触发方法
- (void)segAction:(UISegmentedControl *)seg
{
NSLog(@"index: %ld", seg.selectedSegmentIndex);// 选中下标为2的分段 添加新的分段
if (seg.selectedSegmentIndex == 2) {
[seg insertSegmentWithTitle:@"YIem" atIndex:0 animated:YES];
}
}
pragma mark - 练习
(void)hhlAction:(UISegmentedControl *)hhl
{
NSLog(@"index: %ld", hhl.selectedSegmentIndex);
if (hhl.selectedSegmentIndex == 0) {self.view.backgroundColor = [UIColor redColor];
} else if (hhl.selectedSegmentIndex == 1) {
self.view.backgroundColor = [UIColor yellowColor];
} else if (hhl.selectedSegmentIndex == 2){
self.view.backgroundColor = [UIColor blueColor];
}
}
pragma mark - 滑动控制方法
(void)sliderAction:(UISlider *)slider
{NSLog(@"valie: %f", slider.value);
}
pragma mark - 开关或控制方法
(void)kaiguanAction:(UISwitch *)kaiguan
{
// isOn获取BOOL类型返回值 可以直接作为判断条件
NSLog(@"%d", kaiguan.isOn);
if (kaiguan.isOn) {NSLog(@"开关开启");
}
}
pragma mark - 页码控制触发方法
(void)pageAction:(UIPageControl *)page
{NSLog(@"current: %ld", page.currentPage);
}
///
(void)stepperAction:(UIStepper *)stepper
{NSLog(@" %f",stepper.value);
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.
}
/*
pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.
}
*/
@end