import "AppDelegate.h"
import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];RootViewController *roootVC = [[RootViewController alloc]init];
[self.window setRootViewController:roootVC];
return YES;
}
import "RootViewController.h"
@interface RootViewController ()
@property (nonatomic ,retain)UIImageView *imageView;
@property (nonatomic ,retain)NSArray *array;
@end
@implementation RootViewController
-(UIImageView*)imageView{
if (!_imageView) {
_imageView = [[UIImageView alloc]init];
[_imageView setCenter:self.view.center];
[_imageView setBounds:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:_imageView];
}return _imageView;
}
-(NSArray*)array{
if (!_array) {
_array = [NSArray arrayWithObjects:@"11副本",@"11", nil];
}return _array;
}
-
(void)viewDidLoad {
[super viewDidLoad];// 分段控制器
// items:每個(gè)分段的標(biāo)題(NSArray)
UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithItems:@[@"洛洛",@"鄒杰",@"成崗"]];
// 設(shè)置frame
[segmentControl setFrame:CGRectMake(100, 100, 200, 40)];
// 設(shè)置默認(rèn)被選中的分段
[segmentControl setSelectedSegmentIndex:0];
// 添加觸發(fā)事件(注意ControlEvents點(diǎn)擊方式假栓,每個(gè)分段的操作寻行,是斷在改變的霍掺,使用ValueChanged)
[segmentControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];// 設(shè)置背景顏色
segmentControl.backgroundColor = [UIColor blackColor];
// 設(shè)置表面顏色 字體改變的原因:系統(tǒng)會(huì)根據(jù)當(dāng)前視圖的tintColor去渲染字體或者該視圖的子view的顏色
segmentControl.tintColor = [UIColor whiteColor];
// 給每個(gè)分段設(shè)置圖片
// imageWithRenderingMode:圖片被系統(tǒng)顏色渲染的模式設(shè)置
// UIImageRenderingModeAutomatic, 系統(tǒng)默認(rèn)的渲染方式,它會(huì)根據(jù)當(dāng)前視圖顏色渲染的上下文來自動(dòng)選擇是否要渲染當(dāng)前的圖片拌蜘。目前會(huì)渲染圖片的大多數(shù)是(tintColor)
// UIImageRenderingModeAlwaysOriginal, // 圖片保持自己當(dāng)前的色彩杆烁,不被當(dāng)前視圖顏色所渲染
// UIImageRenderingModeAlwaysTemplate, // 圖片丟棄自己當(dāng)前的顏色,一直被系統(tǒng)顏色渲染
[segmentControl setImage:[[UIImage imageNamed:@"11副本"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forSegmentAtIndex:0];
[segmentControl setImage:[[UIImage imageNamed:@"11"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:1];
// 首次如果我們有默認(rèn)選中狀態(tài)简卧,由于此時(shí)并沒有任何狀態(tài)的改變兔魂,所以才需要手動(dòng)觸發(fā)回調(diào)方法
[self segmentedControlAction:segmentControl];
[self.view addSubview:segmentControl];
//switch開關(guān)
UISwitch *mySwitch = [[UISwitch alloc]initWithFrame:CGRectMake(350, 260, 0, 0)];
[mySwitch addTarget:self action:@selector(switchAction ) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:mySwitch];
//stepper
UIStepper *myStepper = [[UIStepper alloc]initWithFrame:CGRectMake(320, 450, 0, 0)];
myStepper.backgroundColor = [UIColor redColor];
myStepper.tag = 1010;
myStepper.stepValue = 10;
[myStepper addTarget:self action:@selector(stepperAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:myStepper];
// 滑桿 默認(rèn)的value值是0,默認(rèn)范圍是0到1
for (int i = 0; i < 3; i++) {
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(20, 50+50*i, 370, 30)];
// 設(shè)置滑桿的最小值
slider.minimumValue = 0.0;
// 設(shè)置滑桿的最大值
slider.maximumValue = 255.0;
[slider setValue:150.0];
// 最小值處的圖片標(biāo)識(shí)
slider.minimumValueImage = [UIImage imageNamed:@"112"];
// 最大值處的圖片標(biāo)識(shí)
slider.maximumValueImage = [UIImage imageNamed:@"112"];
// 設(shè)置滑桿圖片
[slider setThumbImage:[UIImage imageNamed:@"112"] forState:UIControlStateNormal];
// 設(shè)置已滑過區(qū)域的顏色
[slider setMinimumTrackTintColor:[UIColor yellowColor]];
// 設(shè)置未滑過區(qū)域的顏色
[slider setMaximumTrackTintColor:[UIColor greenColor]];
// 給每個(gè)滑桿添加tag值
slider.tag = 1000+i;
// 添加觸發(fā)事件
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
}
// 通過滑桿放大或縮小圖片
UIImageView *imageView = [[UIImageView alloc]init];
imageView.center = self.view.center;
imageView.bounds = CGRectMake(0, 0, 200, 200);
imageView.image = [UIImage imageNamed:@"11副本"];
imageView.userInteractionEnabled = YES;
imageView.tag = 2000;
[self.view addSubview:imageView];
}
// switch開關(guān)的回調(diào)方法
- (void )switchAction{
NSLog(@"我按了switch開關(guān)");
self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
}
// stepper回調(diào)方法
-(void)stepperAction:(UIStepper*)sender{
UISlider slider = (UISlider)[self.view viewWithTag:1000];
UIStepper step = (UIStepper)[self.view viewWithTag:1010];
// float value = step.value;
slider.value = step.value;
// UIImageView imageView = (UIImageView)[self.view viewWithTag:2000];
// imageView.bounds = CGRectMake(0, 0, 50+value, 50+value);
}
// 滑桿的回調(diào)方法
-(void)sliderAction:(UISlider*)sender{
// 根據(jù)tag值得到三個(gè)滑桿分別開控制紅举娩、綠析校、藍(lán)
UISlider *redSlider = (UISlider*)[self.view viewWithTag:1000];
UISlider *greenSlider = (UISlider*)[self.view viewWithTag:1001];
UISlider *blueSlider = (UISlider*)[self.view viewWithTag:1002];
// 滑桿當(dāng)前所在位置的值
float value = sender.value;
NSLog(@"value*****%f",value);
[self.view setBackgroundColor:[UIColor colorWithRed:redSlider.value/255.0 green:greenSlider.value/255.0 blue:blueSlider.value/255.0 alpha:1]];
// 通過滑桿改變imageView的大小
UIImageView *imageView = (UIImageView*)[self.view viewWithTag:2000];
imageView.bounds = CGRectMake(0, 0, 50+value, 50+value);
}
// 分段選擇器的回調(diào)方法
-(void)segmentedControlAction:(UISegmentedControl*)sender{
// 確認(rèn)目前所選的是哪個(gè)分段,根據(jù)index值
int index = (int)sender.selectedSegmentIndex;
UIColor *bgColor;
switch (index) {
case 0:
bgColor = [UIColor yellowColor];
self.imageView.image = [UIImage imageNamed:self.array[index]];
[self.view addSubview:self.imageView];
break;
case 1:
bgColor = [UIColor greenColor];
self.imageView.image = [UIImage imageNamed:self.array[index]];
[self.view addSubview:self.imageView];
break;
case 2:
bgColor = [UIColor blueColor];
break;
default:
bgColor = [UIColor redColor];
break;
}
self.view.backgroundColor = bgColor;
}
- (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