2017-04-17 3 views
0

を使用して、私はこのようなカスタムUIButton作成:iOSのスウィフト3:カスタムUIButtonはXIB

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ファイルですが、私もそれがここxib

をHomeButton.xibという名前SpringButtonクラスです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) 
} 

} 

:touchesBegan、touchesEnded、touchesMovedは、ときclicクリックボタンをアニメーション化するtouchesCancelled k、しかしそれらの関数は決して呼び出されません。どうして?私はこれを正しくやっているのですか、他の方法で行うべきですか? HomeButtonを使用するには

、私はストーリーボードにIBOutletボタンを作成します。 storyboard

私はカスタムボタンをこのように作成する必要があり、私は後で左と上の別のアイコンに画像を追加しますので、そうですね、私が望むようにボタンをカスタマイズするのは簡単です。

+0

あなたのxibファイルをクラスと同じにすることができます。あなたはSpringButtonとして使用しています。 – karthikeyan

+0

私は見ていますが、Spring Buttonでユーザーとの対話を有効にすると@IBActionを受信できません私のコントローラ内のイベント –

+0

springButtonクラスも追加 – karthikeyan

答えて

0

今のところ、カスタマイズされたボタンのクラスはHomeButtonです。したがって、SpringButtonでオーバーライドする応答関数はありません。

@karthikeyanは、カスタマイズされたボタンのクラスをインスペクタのSpringButtonに変更する必要があるとしています。

+0

ありがとうございます。 UButtonの代わりにUIViewを拡張するようにHomeButtonクラスを変更する必要がありますか? –

関連する問題