ios系列UI篇——UISwitch

来源:互联网 发布:网络大电影 铁扇公主 编辑:程序博客网 时间:2024/05/23 01:24
实在没什么可以多说的,直接上代码吧
#import "ViewController.h"#define screen_width [UIScreen mainScreen].bounds.size.width#define screen_height [UIScreen mainScreen].bounds.size.height@interface ViewController (){    UISwitch * _mySwitch;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    [self createSwitch];}- (void)createSwitch{    float sw = 100;    float sh = 100;    _mySwitch = [[UISwitch alloc] initWithFrame:(CGRectMake((screen_width - sw) / 2, (screen_height - sh) / 2, sw, sh))];    //边框颜色    [_mySwitch setTintColor:[UIColor orangeColor]];    //开启边底色    [_mySwitch setOnTintColor:[UIColor redColor]];    //选择按钮颜色    [_mySwitch setThumbTintColor:[UIColor yellowColor]];    //关闭边底色    _mySwitch.backgroundColor = [UIColor blackColor];        //设置背景图片    [_mySwitch setOnImage:nil];    [_mySwitch setOffImage:nil];        //圆角度    _mySwitch.layer.cornerRadius = 18;    //添加点击事件    [_mySwitch addTarget:self action:@selector(mySwitch) forControlEvents:UIControlEventValueChanged];    [self.view addSubview:_mySwitch];}//监听事件- (void)mySwitch{    if (_mySwitch.isOn == YES)    {        UIAlertView * av = [[UIAlertView alloc] initWithTitle:@"提示" message:@"on" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];        [av show];    }    else if (_mySwitch.isOn == NO)    {        UIAlertView * av = [[UIAlertView alloc] initWithTitle:@"提示" message:@"off" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];        [av show];    }}

0 0