UISwitch(开关)

来源:互联网 发布:linux tomcat log4j 编辑:程序博客网 时间:2024/04/27 19:40
 //创建一个UISwitch对象(开关对象)
    //创建UISwitch对象时,开关按钮的长宽是系统默认的,只可以改变开关的坐标点
    UISwitch *Switch = [[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 50, 30)];
    Switch.backgroundColor = [UIColor redColor];
    [self addSubview:Switch];
    
    //改变开着状态的颜色
    Switch.onTintColor = [UIColor blueColor];
    
    //设置开着状态的图片(iOS7后基本不显示了)
    Switch.onImage = [[UIImage imageNamed:@"3.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
   
    
     Switch.offImage = [[UIImage imageNamed:@"3.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    
    
    //设置关闭状态下背景颜色
    Switch.tintColor = [UIColor cyanColor];
    
    //设置拇指按键的地方的颜色
    Switch.thumbTintColor= [UIColor blackColor];
    
    //设置初始化时开关的状态是开着的
    Switch.on = YES;
    
    //设置初始化时开关的状态,且开着动画
    [Switch setOn:YES animated:YES];
    


点击事件

 //为开关视图绑定事件
    [self.rootView.Switch addTarget:self action:@selector(SwitchAction:) forControlEvents:UIControlEventValueChanged];

//设置开关的点击事件
-(void)SwitchAction:(UISwitch *)sender{
    if (sender.on == YES) {
        NSLog(@"开关开着");
    }
    else{
        NSLog(@"开关关着");
    }
}


0 0
原创粉丝点击