를 사용하여이 같은 사용자 지정있는 UIButton 생성 :아이폰 OS 스위프트 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 파일의를, 나 또한 여기에
을 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는 경우 카릭 버튼을 애니메이션 touchesCancelled k,하지만 그 함수는 호출되지 않습니다. 왜? 올바르게하고 있습니까? 아니면 다른 방법으로해야합니까? 내가 나중에 왼쪽과 다른 아이콘 이미지를 추가 것이기 때문에 사용자 정의 버튼을 이런 식으로 작성해야
을 :
난 그냥 스토리 보드에 함께 IBOutlet 버튼을 만듭니다 homeButton의를 사용하려면 그렇기 때문에 버튼을 원하는대로 쉽게 사용자 정의 할 수 있습니다.
사용자가 해당보기 또는 버튼과 상호 작용할 수있게하고 xib 파일을 클래스와 동일하게 만듭니다. – karthikeyan
오, 알았지 만 스프링 버튼에서 사용자 상호 작용을 활성화하면 @IBAction을 수신 할 수 없습니다. 내 컨트롤러의 이벤트 –
springButton 클래스도 추가 – karthikeyan