程序员iOS DeveloperiOS学习笔记

UIControl的几个子类

2016-03-25  本文已影响146人  每日总结

segment分栏视图
slider滑块
switch开关
stepper步进式控制器

UISegmentedControl

分栏视图用于在多个界面中切换;

@interface RootViewController ()
@property (nonatomic, retain) UIViewController *first;
@property (nonatomic, retain) UIViewController *two;
@end
@implementation RootViewController 
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
- (void)initChildViewController{//这种方法设置界面在切换时会有移动的动画效果,也可以把x都设在0,然后通过控制hidden来控制切换
    self.first = [[UIViewController alloc] init];
    self.first.view.frame = CGRectMake(kScreenWidth*0,0,kScreenWidth,kScreenHeight);
    self.first.view.backgroundColor = [UIColor white];
    [self.view addSubview:_first.view];
    [self addChildViewController:_first];
    [_first release];
    
    self.two = [[UIViewController alloc] init];
    self.two.view.frame = CGRectMake(kScreenWidth*1,0,kScreenWidth,kScreenHeight);
    self.two.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:_two.view];
    [self addChildViewController:_two];
    [_two release];
}
- (void)viewDidLoad{
    [super viewDidLoad];
    [self initChildViewController];
    //初始化方法,Items是一个分栏的标题数组
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"登陆",@"注册",@"忘记密码"];
    //宽度要和标题个数以及标题长度相配合 否则有时会显示不完整
    segment.frame = CGRectMake(57,100,300,30);
    //初始分栏下标,从左到右从0开始
    segment.selectedSegmentIndex = 0;
    //主题颜色
    segment.tintColor = [UIColor blueColor];
    segment.backgroundColor = [UIColor whiteColor];
    //设置点击分栏后是否会自动弹起,默认是NO,不弹起
    segment.momentary = NO;
    //添加事件
    [segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];//这几个control控件的触发都是ValueChanged
    [self.view addSubview:segment];
    [segment release];
}
- (void)segmentAction:(UISegmentedControl *)segment{
    [UIView animatedWithDuration:1 animations:^{
        self.first.view.frame = CGRectMake(kScreenWidth*(0-segment.selectedSegmentIndex),0,kScreenWidth,kScreenHeight);
        self.two.view.frame = CGRectMake(kScreenWidth*(1-segment.selectedSegmentIndex),0,kScreenWidth,kScreenHeight);
    } completion:nil];    
}
@end
分栏视图.png

UISlider滑块

- (void)viewDidLoad{
    [super viewDidLoad]; 
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(50,161,100,30)];
    //如果不将slider竖起来,高度设置不会影响显示但是会影响操作的区域大小;
    [self.view addSubview:slider];
    [slider release];
    //设置小于当前值滑条的颜色
    slider.minimumTrackTintColor = [UIColor redColor];
    //设置大于当前值滑条的颜色
    slider.maximumTrackTintColor = [UIColor greenColor];
    //设置滑块的颜色
    slider. thumbTintColor = [UIColor cyanColor];
    //设置初始值 根据然后最大值和最小值计算滑块位置;
    slider.value = 0;
    //设置最大值 
    slider.maximumValue = 0.9;// 默认是1
    //设置最小值
    slider.minimumValue = 0;// 默认是0
    //设置方向
    slider.transform = CGAffineTransformMakeRotation(M_PI_2*3);
    //M_PI相当于以一个180度,我们要让它竖起来 并且让最大值在上边需要90度*3 ,M_PI_2为90度
    slider.frame = CGRectMake(50,161,30,414);
    //改了transform后 需要重新设置frame,此时的height属性会变成slider的长度;
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    //改变触发方式,默认是YES,只要移动就触发,NO是移动结束松手后触发
    slider.continuous = YES;
}
- (void)sliderAction:(UISlider *)slider{
    slider.superview.alpha = 1-slider.value;    
}
slider.png

UISwitch开关

- (void)viewDidLoad{
    [super viewDidLoad];
    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(100,100,100,100)];
    testView.backgroundColor = [UIColor redColor];
    [self.view addSubview:testView];
    [testView release];

    UISwitch *swc = [[UISwitch alloc] initWithFrame:CGRectMake(50,50,50,50)];//大小是固定的
    [self.view addSubview:swc];
    //开关状态 默认是关闭
    swc.on = YES
    [swc addTarget:self action:@selector(swcAction:) forControlEvents:UIControlEventValueChanged];
    [swc release];
}
- (void)swcAction:(UISwitch *)swc{
    self.view.subviews[0].hidden = swc.on;
}
开关.png

UIStepper

- (void)viewDidLoad{
    [super viewDidLoad];
    UIView *test = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
    [self.view addSubview:test];
    [test release];
    UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(100,100,30,30)];// 大小是系统固定的
    stepper addTarget:self action:@selector(stepperAction:) forControlEvents:UIControlEventValueChanged];
    stepper.stepValue = 0.05;// 每点击一次按钮发生的变化量,默认为1
    stepper.value = 1; // 初始量,默认为0,如果设为0,最开就不能点击减号按钮
    stepper.continuous = YES;// 设置是否连续变化,默认是YES,如果设为NO,只会在抬起手指时�一次性改变value值
    stepper.autorepeat = YES;// 设置是否重复触发,默认是YES,如果设为NO,按下到抬起之间只会触发一次
    [self.view addSubview:stepper];
    [stepper release];
}

- (void)stepperAction:(UIStepper *)stepper{
    UIView *test = self.subviews[0];
    test.transform = CGAffineTransformScale(test.transform,stepper.value, stepper.value);
    stepper.value = 1;// 不置为1的话,调节大小会出现延迟,具体原因请参照《相册相机以transform》
}
stepper.png
上一篇下一篇

猜你喜欢

热点阅读