UIButton+WCreatButton.swift
import UIKit
extension UIButton {
//MARK: - Title
func setNormal(title: String?) {
self.setTitle(title, for: UIControlState.normal)
}
func normalTitle() -> String? {
return title(for: UIControlState.normal)
}
func setSelected(title: String?) {
self.setTitle(title, for: UIControlState.selected)
}
func selectedTitle() -> String? {
return title(for: UIControlState.selected)
}
func setHighlighted(title: String?) {
self.setTitle(title, for: UIControlState.highlighted)
}
func highlightedTitle() -> String? {
return title(for: UIControlState.highlighted)
}
//MARK: - TitleColor
func setNormal(titleColor: UIColor?) {
self.setTitleColor(titleColor, for: UIControlState.normal)
}
func normalTitleColor() -> UIColor? {
return titleColor(for: UIControlState.normal)
}
func setSelected(titleColor: UIColor?) {
self.setTitleColor(titleColor, for: UIControlState.selected)
}
func selectedTitleColor() -> UIColor? {
return titleColor(for: UIControlState.selected)
}
func setHighlighted(titleColor: UIColor?) {
self.setTitleColor(titleColor, for: UIControlState.highlighted)
}
func highlightedTitleColor() -> UIColor? {
return titleColor(for: UIControlState.highlighted)
}
//MARK: - Image
func setNormal(image: UIImage?) {
self.setImage(image, for: UIControlState.normal)
}
func normalImage() -> UIImage? {
return image(for: UIControlState.normal)
}
func setSelected(image: UIImage?) {
self.setImage(image, for: UIControlState.selected)
}
func selectedImage() -> UIImage? {
return image(for: UIControlState.selected)
}
func setHighlighted(image: UIImage?) {
self.setImage(image, for: UIControlState.highlighted)
}
func highlightedImage() -> UIImage? {
return image(for: UIControlState.highlighted)
}
//MARK: - BackgroundImage
func setNormal(backgroundImage: UIImage?) {
self.setBackgroundImage(backgroundImage, for: UIControlState.normal)
}
func normalBackgroundImage() -> UIImage? {
return backgroundImage(for: UIControlState.normal)
}
func setSelected(backgroundImage: UIImage?) {
self.setBackgroundImage(backgroundImage, for: UIControlState.selected)
}
func selectedBackgroundImage() -> UIImage? {
return backgroundImage(for: UIControlState.selected)
}
func setHighlighted(backgroundImage: UIImage?) {
self.setBackgroundImage(backgroundImage, for: UIControlState.highlighted)
}
func highlightedBackgroundImage() -> UIImage? {
return backgroundImage(for: UIControlState.highlighted)
}
/// 圆角
///
/// - Parameters:
/// - borderWidth: 边框宽度
/// - borderColor: 边框颜色
/// - cornerRadius: 圆角半径
func set(borderWidth: CGFloat?, borderColor: UIColor?, cornerRadius: CGFloat?) {
layer.shouldRasterize = true
layer.rasterizationScale = UIScreen.main.scale
layer.masksToBounds = true
layer.borderWidth = borderWidth!
layer.borderColor = borderColor?.cgColor
layer.cornerRadius = cornerRadius!
}
/// 快速创建按钮(全)
///
/// - Parameters:
/// - imageName: 图片
/// - titleColor: 字体颜色
/// - titleFont: 字体大小
/// - backgroundColor: 背景颜色
/// - title: 标题
/// - Returns: button
static func buttonWith(imageName: String?, titleColor: UIColor?, titleFont: UIFont?, backgroundColor: UIColor?, title: String?) -> UIButton {
let button = UIButton(type: .custom)
button.backgroundColor = backgroundColor
if title?.count != nil {
button.setTitle(title, for: UIControlState.normal)
button.setTitleColor(titleColor, for: UIControlState.normal)
}
if titleFont != nil {
button.titleLabel?.font = titleFont
}
if (imageName?.count) != nil {
button.setImage(UIImage(named: imageName!), for: UIControlState.normal)
if title?.count != nil {
button.titleEdgeInsets = UIEdgeInsetsMake(0, UIScreen.main.bounds.size.width / 375, 0, 0)
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, UIScreen.main.bounds.size.width / 375)
}
}
return button
}
/// 快速创建按钮(无图片)
///
/// - Parameters:
/// - titleColor: 字体颜色
/// - titleFont: 字体大小
/// - backgroundColor: 背景颜色
/// - title: 标题
/// - Returns: button
static func buttonWith(titleColor: UIColor?, titleFont: UIFont?, backgroundColor: UIColor?, title: String?) -> UIButton {
return UIButton.buttonWith(imageName: nil, titleColor: titleColor, titleFont: titleFont, backgroundColor: backgroundColor, title: title)
}
/// 快速创建按钮(图片)
///
/// - Parameters:
/// - imageName: 图片
/// - backgroundColor: 背景颜色
/// - Returns: button
static func buttonWith(imageName: String?,backgroundColor: UIColor?) -> UIButton {
return UIButton.buttonWith(imageName: imageName, titleColor: nil, titleFont: nil, backgroundColor: backgroundColor, title: nil)
}
/// 快速创建按钮(背景颜色)
///
/// - Parameters:
/// - backgroundColor: 背景颜色
/// - Returns: button
static func buttonWith(backgroundColor: UIColor?) -> UIButton {
return UIButton.buttonWith(imageName: nil, titleColor: nil, titleFont: nil, backgroundColor: backgroundColor, title: nil)
}
}
extension UIButton {
/// 倒计时
///
/// - Parameters:
/// - timeLine: 倒计时总时间
/// - title: 还没倒计时的title
/// - mainColor: 还没倒计时的颜色
/// - countColor: 倒计时的颜色
func startWith(timeLine: TimeInterval, title: String?, mainColor:UIColor?, countColor: UIColor?) {
var time: Int = Int(timeLine)
let codeTimer = DispatchSource.makeTimerSource(flags: .init(rawValue: 0), queue: DispatchQueue.global())
codeTimer.schedule(deadline: .now(), repeating: .milliseconds(1000)) //此处方法与Swift 3.0 不同
codeTimer.setEventHandler(handler: {
time -= 1
DispatchQueue.main.async {
self.isUserInteractionEnabled = false
}
if time < 0 {
codeTimer.cancel()
DispatchQueue.main.async {
self.isUserInteractionEnabled = true
self.setTitle(title, for: UIControlState.normal)
self.setTitleColor(mainColor, for: UIControlState.normal)
self.layer.borderColor = mainColor?.cgColor
}
return
}
DispatchQueue.main.async {
self.setTitle("\(time)", for: UIControlState.normal)
self.setTitleColor(countColor, for: UIControlState.normal)
self.layer.borderColor = countColor?.cgColor
}
})
codeTimer.activate()
}
}
UIButton+ClickEventBlock.swift
import UIKit
/**
点击时间Block 回调
@param button 点击的btn
*/
typealias clickEventBlock = (_ button : UIButton) ->Void
var buttonEventBlock : clickEventBlock?
extension UIButton {
/// 对象点击回调的方法
///
/// - Parameter block: 回调
func addClickEventWithBlock(block : @escaping clickEventBlock) -> Void {
buttonEventBlock = block
if (buttonEventBlock != nil) {
self.addTarget(self, action: #selector(btnTouchUpInSideEvent(button:)), for: UIControlEvents.touchUpInside)
}else {
self.removeTarget(self, action: #selector(btnTouchUpInSideEvent(button:)), for: UIControlEvents.touchUpInside)
}
}
@objc func btnTouchUpInSideEvent(button : UIButton) {
if (buttonEventBlock != nil) {
buttonEventBlock!(button)
}
}
var block : clickEventBlock {
get {
return objc_getAssociatedObject(self, "key") as! clickEventBlock
}
set {
objc_setAssociatedObject(self, "key", buttonEventBlock, .OBJC_ASSOCIATION_COPY_NONATOMIC)
}
}
}