• iOS开发技巧


    1. 初始化加载到视图界面

    (Swift)

    import UIKit
    
    class ViewController: UIViewController {
        // 1. create a property of type UISwitch
        var mainSwitch:UISwitch!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            // 2. create switch
            mainSwitch = UISwitch(frame: CGRect(x: 100, y: 100,  0, height: 0))
            view.addSubview(mainSwitch)
        }
    }

    (Objective-C)

    #import "ViewController.h"
    
    @interface ViewController ()
    
    // 1. create a property of type UISwitch
    @property (nonatomic, strong) UISwitch *mainSwitch;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 2. create switch
        self.mainSwitch = [[UISwitch alloc] initWithFrame:
            CGRectMake(100, 100, 0, 0)];
        [self.view addSubview:self.mainSwitch];
    }
    
    @end

    2. 设置开关状态

    (Swift)

    mainSwitch.setOn(true, animated: true)

    (Objective-C)

    [self.mainSwitch setOn:YES];

    3. 判断开关状态

    (Swift)

    if mainSwitch.on{
        /* Switch is on */
    } else {
        /* Switch is off */
    }

    (Objective-C)

    if ([self.mainSwitch isOn]){
        NSLog(@"The switch is on.");
    } else {
        NSLog(@"The switch is off.");
    }

    4. 添加事件监听

    (Swift)

    mainSwitch.addTarget(self,
        action: "switchIsChanged:",
        forControlEvents: .ValueChanged)
        
    func switchIsChanged(sender: UISwitch) {
        println("Sender is = (sender)")
        if sender.on{
            println("The switch is turned on")
        } else {
            println("The switch is turned off")
        }
    }

    (Objective-C)

    [self.mainSwitch 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.");
        }
    }

    5. 定制开关UI

    /* Adjust the off-mode tint color */
    mainSwitch.tintColor = UIColor.redColor()
    
    /* Adjust the on-mode tint color */
    mainSwitch.onTintColor = UIColor.brownColor()
    
    /* Also change the knob's tint color */
    mainSwitch.thumbTintColor = UIColor.greenColor()
  • 相关阅读:
    jsoup
    【伪装位置神器】神行者AnyLocation 1.3.0001可用于微信,陌陌
    MD5 哈希等各种加密方式 都是对这个对象进行各种运算, 然后得出1个字符串
    【html】param 以及 embed 的有关 flash 属性详解
    【css】绝对定位的元素在 ie6 下不显示
    【javascript】浮点数运算问题分析及解决方法
    【jquery】邮箱自动补全 + 上下翻动
    【javascript】设为首页——setHome
    【javascript】js 检验密码强度
    【jquery】jquery 自定义滚动条
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/7268547.html
Copyright © 2020-2023  润新知