jinzhubao/jinzhubao/Business/home/View/CustomButton.swift

(完整代码) 1KB

//
//  CustomButton.swift
//  jinzhubao
//
//  Created by CMCC on 2019/1/31.
//  Copyright © 2019 CMCC. All rights reserved.
//

import UIKit

class CustomButton: UIView {
    let imageButton = UIButton(type: UIButton.ButtonType.custom)
    let label = UILabel()
    var action : (()->())?
    convenience init(frame: CGRect, title: String, image:UIImage) {
        self.init(frame: frame)
        
        self.layer.cornerRadius = 5
        self.layer.masksToBounds = true
        
        imageButton.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height / 13 * 8)
        imageButton.setImage(image, for: UIControl.State.normal)
        imageButton.isUserInteractionEnabled = false
        imageButton.imageView?.contentMode = .scaleAspectFit
        addSubview(imageButton)
        label.frame = CGRect(x: 0, y: frame.height / 13 * 8, width: frame.width, height: frame.height / 13 * 5)
        label.text = title
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 13)
        addSubview(label)
        
        let tapgr = UITapGestureRecognizer(target: self, action: #selector(tapAction(_:)))
        self.addGestureRecognizer(tapgr)
    }
    
    @objc private func tapAction(_ gr:UITapGestureRecognizer){
        if action != nil {
            action!()
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.backgroundColor = UIColor.colorHex("#eeeeee")
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.backgroundColor = UIColor.colorHex("#ffffff")
    }
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.backgroundColor = UIColor.colorHex("#ffffff")
    }
}

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。