• UI: UISwitch 创建及使用开关 定制开关


    1.UISwitch类提供一个开/关的用户控制.

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.mySwitch = [[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 0, 0)];
        [self.view addSubview:self.mySwitch];
    }
    开关控件的预设状态是关闭。可以透过改变 UISiwtch 实例的 on 属性来改变。另外也可以呼叫 setOn 方法,如下: 
    [self.mySwitch setOn:YES];
    若希望在开关控件被打开或关闭时得到通知信息,就必须在你的类中,利用 UISwitch 的addTarget:action:forControlEvents:方法加上开关的 target。 
    [self.mySwitch addTarget:self action:@selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];
    
    - (void)switchIsChanged:(UISwitch *)paramSender{
        NSLog(@"Sender is = %@",paramSender);
        if ([paramSender isOn]) {
            NSLog(@"The switch is turned on");
        }else{
            NSLog(@"The switch is turned off");
        }
    }

    2.

    开关的定制主要有两个方法:
    Tint Colors:可以应用在 UI 组件上,例如 UISwitch。tint 颜色会被应用在UI 组件当前颜色的 top 位置之上。例如,一个普通的 UISwitch,可以设置不同的颜色。当将 tint 颜色应用在 UI 组件上时,控件的原始颜色会被 tint 颜色最小化。
    Images:开关有两个图片:
    On Image:这个图片代表了开关的开状态.宽度是 77 points,高度是 22. 
    Off Image:这个图片代表了开关的关状态.宽度是 77 points,高度是 22. (宽高度不一定了)

    2.1 修改开关的 tint 颜色。可以通过使用 UISwitch 类的 3 个重要属性来完成: 

    tinColor:这个 tint color 会被用在开关的 off 状态上。不幸的是,苹果没有将其名字用offTintColor :代替 tintColor。这个属性的类型是 UIColor。

    thumbTintColor:这个 tint color 被用在开关的小圆钮上,这个属性是 UIColor。

    onTintColor:这个 tint color 被用在开关的 on 状态上,这个属性也是 UIColor。 

     例子:

    //将开关的 on-mode tint color 设置为红色,off-mode tint color 设置为棕色(brown),小圆钮的 tint color 则为绿色
        _mySwitch.tintColor = [UIColor redColor];
        _mySwitch.onTintColor = [UIColor greenColor];
        _mySwitch.thumbTintColor = [UIColor brownColor];
    //switch的开关图片
        _mySwitch.onImage = [UIImage imageNamed:@"on"];
        _mySwitch.offImage = [UIImage imageNamed:@"off"];
     
  • 相关阅读:
    [LeetCode] Wildcard Matching, Solution
    [LeetCode] Add Binary 解题报告
    [LeetCode] Validate Binary Search Tree 解题报告
    [LeetCode] ZigZag Conversion 解题报告
    [LeetCode] Best Time to Buy and Sell Stock II Solution
    [LeetCode] Anagrams 解题报告
    [LeetCode] Word Search 解题报告
    【转载】git/github初级运用自如
    关于实训的那点事儿
    【转载】解决git Push时请求username和password,而不是sshkey验证
  • 原文地址:https://www.cnblogs.com/safiri/p/4013425.html
Copyright © 2020-2023  润新知