簡體   English   中英

iOS Swift 3:使用xib自定義UIButton

[英]iOS Swift 3: Custom UIButton using xib

我創建了一個這樣的自定義UIButton:

import UIKit

class HomeButton: UIButton {

    @IBOutlet weak var viewBG: UIView!

    @IBInspectable var fontSize: CGFloat = 22

    @IBInspectable var bgColor: UIColor? = UIColor.white{
        didSet{
            viewBG.backgroundColor = bgColor
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        self.titleLabel?.font = UIFont(name: "Lato-Bold", size: fontSize)
    }


    func setup() {
        let view = loadViewFromNib(nibName: "HomeButton") as! SpringButton
        view.frame = self.bounds
        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        addSubview(view)

        self.clipsToBounds = true

        self.titleLabel?.textColor = UIColor.white
        self.setTitleColor(UIColor.white, for: UIControlState.normal)

        self.layer.cornerRadius = CGFloat(K.CORNER_RADIUS)  

        self.backgroundColor = UIColor.clear
    }

}

這是我的HomeButton的xib文件,我也把它命名為HomeButton.xib 廈門國際銀行

這是SpringButton類

import UIKit

class SpringButton: UIButton {

   open var minimumScale: CGFloat = 0.95
   open var pressSpringDamping: CGFloat = 0.4
   open var releaseSpringDamping: CGFloat = 0.35
   open var pressSpringDuration = 0.4
   open var releaseSpringDuration = 0.5

public init() {
    super.init(frame: CGRect.zero)
}

required public init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
}

override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)
    UIView.animate(withDuration: self.pressSpringDuration, delay: 0, usingSpringWithDamping: self.pressSpringDamping, initialSpringVelocity: 0, options: [.curveLinear, .allowUserInteraction], animations: { () -> Void in
        self.transform = CGAffineTransform(scaleX: self.minimumScale, y: self.minimumScale)
    }, completion: nil)
}

override open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
    UIView.animate(withDuration: self.releaseSpringDuration, delay: 0, usingSpringWithDamping: self.releaseSpringDamping, initialSpringVelocity: 0, options: [.curveLinear, .allowUserInteraction], animations: { () -> Void in
        self.transform = CGAffineTransform.identity
    }, completion: nil)
}

override open func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    let location = touches.first!.location(in: self)
    if !self.bounds.contains(location) {
        UIView.animate(withDuration: self.releaseSpringDuration, delay: 0, usingSpringWithDamping: self.releaseSpringDamping, initialSpringVelocity: 0, options: [.curveLinear, .allowUserInteraction], animations: { () -> Void in
            self.transform = CGAffineTransform.identity
        }, completion: nil)
    }
}

override open func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesCancelled(touches, with: event)
    UIView.animate(withDuration: self.releaseSpringDuration, delay: 0, usingSpringWithDamping: self.releaseSpringDamping, initialSpringVelocity: 0, options: [.curveLinear, .allowUserInteraction], animations: { () -> Void in
        self.transform = CGAffineTransform.identity
    }, completion: nil)
}

}

在SpringButton中,我重寫函數:touchesBegan,touchesEnded,touchesMoved,touchesCancelled在單擊時為按鈕設置動畫,但是從不調用這些函數。 為什么? 我這樣做是正確的還是我應該采取其他方式?

要使用HomeButton,我只需在故事板中創建一個IBOutlet按鈕: 故事板

我需要以這種方式創建自定義按鈕,因為我稍后會在左側添加圖像,在右側添加另一個圖標,因此可以按照我想要的方式自定義按鈕。

現在,您的自定義按鈕的類是HomeButton因此它不響應您在SpringButton覆蓋的SpringButton

正如@karthikeyan所說,你需要在檢查員SpringButton自定義按鈕的類更改為SpringButton

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM