UI基础 UISwitch +UISlider

root m

#import "RootViewController.h"

@interface RootViewController ()
{
    UITextField* tf;
}
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //开关 UISwitch
    UISwitch *sw=[[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 0, 0)];
    [self.view addSubview:sw];
    //设置开关打开颜色
    sw.onTintColor=[UIColor yellowColor];
    //设置小球颜色
    sw.thumbTintColor=[UIColor orangeColor];
    
    sw.tintColor=[UIColor redColor];
    //给开关添加一个事件
    [sw addTarget:self action:@selector(touchSwitch) forControlEvents:UIControlEventValueChanged];
    
    tf=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 260, 40)];
    
    tf.backgroundColor=[UIColor redColor];
    [self.view addSubview:tf];
    
    //滑动条
    UISlider *sl =[[UISlider alloc]initWithFrame:CGRectMake(100, 400, 260, 20)];
    sl.backgroundColor=[UIColor purpleColor];
    [self.view addSubview:sl];
    
    //小球颜色
    sl.thumbTintColor=[UIColor redColor];
    //为划过区域
    sl.maximumTrackTintColor=[UIColor greenColor];
    //划过区域
    sl.minimumTrackTintColor=[UIColor blueColor];
    
    //设置最大最小值
    sl.maximumValue=0.0;
    sl.maximumValue=100.0;
    //添加事件
    [sl addTarget:self action:@selector(sliderMe:) forControlEvents:UIControlEventValueChanged];
    
    
    
    
     
}
//滑动模块时候调用方法
-(void)sliderMe:(UISlider *)s
{
    
    NSLog(@"%f",s.value);
}


//点击开关执行的方法

-(void)touchSwitch:(UISwitch *)s
{
    
//    if(s.on){
//        tf.enabled=YES;
//    }else {
//
//        tf.enabled=NO;
//    }
    
    tf.enabled=s.on;
    
}

@end

猜你喜欢

转载自www.cnblogs.com/zhangqing979797/p/13400019.html