UISwitch - 开关按钮 的使用详解

https://blog.csdn.net/catstarxcode/article/details/53541048#

//
//  ViewController.m
//  UISwitch滑块切换开关
//
//  Created by Liu,Wenbo(TBRD) on 2020/5/28.
//  Copyright © 2020 Liu,Wenbo(TBRD). All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    UISwitch *switchButton;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //初始化UISwitch滑块
    switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    
    //设置滑块的背景颜色
    //switchButton.backgroundColor = [UIColor orangeColor];

    //设置按钮开启的颜色 图片
    switchButton.onTintColor = [UIColor greenColor];
    //switchButton.onImage = [UIImage imageNamed:@""];//已弃用


    //设置关闭颜色 图片
    switchButton.tintColor = [UIColor redColor];
    //switchButton.offImage = [UIImage imageNamed:@""];//已弃用

    //设置中心按钮的颜色
    //switchButton.thumbTintColor = [UIColor purpleColor];
    
    //设置开关
    //switchButton.on = YES;
    
    
#pragma  mark - 添加点击事件
    [switchButton addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];// UIControlEventValueChanged值是否改变
    
    //添加到视图
    [self.view addSubview:switchButton];
    
}
//点击事件处理方法
-(void)action:(UISwitch *)uiswitch{
    NSLog(@"开关是否开启:%d",uiswitch.isOn);//
}
//点击空白处的事件
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [switchButton setOn:!switchButton.on animated:YES];
}

@end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值