UISegmentedControl、UISlider、UISwitch、UIStepper

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:每个分段的标题(NSArray)
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithItems:@[@"洛洛",@"邹杰",@"成岗"]];
    // 设置frame
    [segmentControl setFrame:CGRectMake(100, 100, 200, 40)];
    // 设置默认被选中的分段
    [segmentControl setSelectedSegmentIndex:0];
    // 添加触发事件(注意ControlEvents点击方式,每个分段的操作,是断在改变的,使用ValueChanged)
    [segmentControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];

    // 设置背景颜色
    segmentControl.backgroundColor = [UIColor blackColor];
    // 设置表面颜色 字体改变的原因:系统会根据当前视图的tintColor去渲染字体或者该视图的子view的颜色
    segmentControl.tintColor = [UIColor whiteColor];
    // 给每个分段设置图片
    // imageWithRenderingMode:图片被系统颜色渲染的模式设置

// UIImageRenderingModeAutomatic, 系统默认的渲染方式,它会根据当前视图颜色渲染的上下文来自动选择是否要渲染当前的图片。目前会渲染图片的大多数是(tintColor)

// UIImageRenderingModeAlwaysOriginal, // 图片保持自己当前的色彩,不被当前视图颜色所渲染
// UIImageRenderingModeAlwaysTemplate, // 图片丢弃自己当前的颜色,一直被系统颜色渲染
[segmentControl setImage:[[UIImage imageNamed:@"11副本"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forSegmentAtIndex:0];
[segmentControl setImage:[[UIImage imageNamed:@"11"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:1];
// 首次如果我们有默认选中状态,由于此时并没有任何状态的改变,所以才需要手动触发回调方法
[self segmentedControlAction:segmentControl];
[self.view addSubview:segmentControl];

//switch开关
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];

//  滑杆   默认的value值是0,默认范围是0到1
    for (int i = 0; i < 3; i++) {
    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(20, 50+50*i, 370, 30)];
    //  设置滑杆的最小值
    slider.minimumValue = 0.0;
    //  设置滑杆的最大值
    slider.maximumValue = 255.0;
    [slider setValue:150.0];
    //  最小值处的图片标识
    slider.minimumValueImage  = [UIImage imageNamed:@"112"];
    //  最大值处的图片标识
    slider.maximumValueImage = [UIImage imageNamed:@"112"];
    //  设置滑杆图片
    [slider setThumbImage:[UIImage imageNamed:@"112"] forState:UIControlStateNormal];
    //  设置已滑过区域的颜色
    [slider setMinimumTrackTintColor:[UIColor yellowColor]];
    //  设置未滑过区域的颜色
    [slider setMaximumTrackTintColor:[UIColor greenColor]];
    //  给每个滑杆添加tag值
    slider.tag = 1000+i;
    //   添加触发事件
    [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开关的回调方法

  • (void )switchAction{
    NSLog(@"我按了switch开关");
    self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
    }

// stepper回调方法
-(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);

}

// 滑杆的回调方法
-(void)sliderAction:(UISlider*)sender{

//  根据tag值得到三个滑杆分别开控制红、绿、蓝
UISlider *redSlider = (UISlider*)[self.view viewWithTag:1000];
UISlider *greenSlider = (UISlider*)[self.view viewWithTag:1001];
UISlider *blueSlider = (UISlider*)[self.view viewWithTag:1002];

//  滑杆当前所在位置的值
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);

}

// 分段选择器的回调方法
-(void)segmentedControlAction:(UISegmentedControl*)sender{
// 确认目前所选的是哪个分段,根据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

你可能感兴趣的:(UISegmentedControl、UISlider、UISwitch、UIStepper)