UIControl

self.view.backgroundColor = [UIColor whiteColor];

//UIControl 控制类

//addTarget:action:forControlEvents

//添加响应事件(满足什么条件下 让某人调用某方法)

/********UISegmentedControl 分段控制器**********/

UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"消息",@"电话",@"微信"]];

seg.frame = CGRectMake(100, 100, 200, 40);

[self.view addSubview:seg];

[seg release];

//选中分段下标

seg.selectedSegmentIndex = 0;

//背景颜色

//    seg.backgroundColor = [UIColor blackColor];

//渲染颜色

seg.tintColor = [UIColor lightGrayColor];

//插入新的分段

//    [seg insertSegmentWithImage:@"陌陌" atIndex:2 animated:YES];

//添加响应事件(通过下标值的变化触发方法)

[seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];

// red

self.redV = [[UIView alloc] initWithFrame:CGRectMake(50, 200, 300, 300)];

self.redV.backgroundColor = [UIColor redColor];

[self.view addSubview:self.redV];

[_redV release];

//greenV

self.greenV = [[UIView alloc] initWithFrame:CGRectMake(50, 200, 300, 300)];

self.greenV.backgroundColor = [UIColor greenColor];

[self.view addSubview:self.greenV];

[_greenV release];

/*****UISlider 滑块控制器 ******************/

UISlider *sl = [[UISlider alloc] initWithFrame:CGRectMake(50, 50, 200, 50)];

sl.backgroundColor = [UIColor yellowColor];

[self.view addSubview:sl];

[sl release];

//颜色设置

//滑过距离的颜色(滑块左)

sl.minimumTrackTintColor = [UIColor blackColor];

//未滑过距离的颜色(滑块右)

sl.maximumTrackTintColor = [UIColor redColor];

//滑块颜色

sl.thumbTintColor = [UIColor greenColor];

//添加响应事件

[sl addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];

//滑动范围

//最小值

sl.minimumValue = -100;

//最大值

sl.maximumValue = 1000;

//更新滑块起始点

sl.value = -100;

/************ UIPageControl 页码控制器 ******************/

UIPageControl *pc = [[UIPageControl alloc] initWithFrame:CGRectMake(50, 150, 100, 50)];

pc.backgroundColor = [UIColor blueColor];

[self.view addSubview:pc];

[pc release];

//页数

pc.numberOfPages = 5;

//当前页

pc.currentPage = 2;

//颜色

//页码颜色

pc.pageIndicatorTintColor = [UIColor redColor];

//当前页码颜色

pc.currentPageIndicatorTintColor = [UIColor yellowColor];

//响应事件

[pc addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

/************* UISwicth 开关 ****************/

UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(250, 150, 150, 50)];

sw.backgroundColor = [UIColor whiteColor];

[self.view addSubview:sw];

[sw release];

//开关属性

sw.on = YES;

sw.onTintColor = [UIColor redColor];

sw.tintColor = [UIColor blueColor];

sw.thumbTintColor = [UIColor yellowColor];

//响应方法

[sw addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];

}

#pragma mark - 开关

-(void)switchAction:(UISwitch *)sw{

NSLog(@"%d",sw.on);

if (sw.on) {

NSLog(@"开启");

}else{

NSLog(@"关闭");

}

}

#pragma mark - 页码控制器

-(void)pageAction:(UIPageControl *)page{

NSLog(@"%ld",page.currentPage);

}

#pragma mark - 滑块控制器

-(void)sliderAction:(UISlider *)sl{

NSLog(@"%f",sl.value);

}

#pragma mark - 分段控制器

-(void)segAction:(UISegmentedControl *)seg{

//获取视图对象的方式

//1.tag值

//2.属性

if (seg.selectedSegmentIndex == 0) {

//transition 过度动画

//参数一:开始视图

//参数二:结束视图

//参数三:持续时间

//参数四:动画选项

//参数五:完成动画之后调用的block

[UIView transitionFromView:self.greenV toView:self.redV duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom completion:^(BOOL finished) {

}];

}

if (seg.selectedSegmentIndex == 1) {

[UIView transitionFromView:self.redV toView:self.greenV duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom completion:^(BOOL finished) {

}];

}

NSLog(@"%ld",seg.selectedSegmentIndex);

}

你可能感兴趣的:(UIControl)