美文网首页
Swift 中使用Selector

Swift 中使用Selector

作者: 贼噶人 | 来源:发表于2019-06-17 18:28 被阅读0次
//
//  ViewController.swift
//  Demo
//
//  Created by gang.zhou on 2018/9/22.
//  Copyright © 2018年 gang.zhou. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    private var button:UIButton? = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
        view.backgroundColor = UIColor.red
        button = UIButton.init(frame: CGRect.init(x: (view.frame.width * 0.1), y: (view.frame.height - 40)/2, width: (view.frame.width * 0.8), height: 40))
        button!.setTitle("This is button,click me!", for: .normal)
        button!.backgroundColor = UIColor.yellow
        button!.setTitleColor(UIColor.red, for: .normal)
        view.addSubview(button!)
        button!.addTarget(self, action:(#selector(touchDown)), for: UIControl.Event.touchDown)
        button!.addTarget(self, action:(#selector(touchUp)), for: UIControl.Event.touchUpInside)
        button!.addTarget(self, action:(#selector(touchUp)), for: UIControl.Event.touchUpOutside)
        
    }

    @objc func touchDown(){
        if let button = button {
            button.backgroundColor = UIColor.cyan
        }
        
    }
    
    @objc func touchUp(){
        if let button = button {
            button.backgroundColor = UIColor.yellow
        }
    }
    
}

相关文章

网友评论

      本文标题:Swift 中使用Selector

      本文链接:https://www.haomeiwen.com/subject/dbuzfctx.html