我已经创建了一个自定义UI按钮类,并且我正在尝试在我的UIViewController类中为这些按钮添加特定的目标,因为我知道我不能这样做。这是我到目前为止所知道的:
class CustomButton: UIButton{
override init(frame: CGRect){
super.init(frame: frame)
setConfig()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setConfig()
}
func setConfig(){
backgroundColor = UIColor(named: "SpecialGreen")
layer.borderColor = UIColor.darkGray.cgColor
layer.borderWidth = 1
layer.cornerRadius = 5.0
layer.zPosition = 2
titleLabel?.font = UIFont(name: "PingFangSC-Semibold", size: 15)
}
func touchIn(){
UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction,.curveEaseIn], animations: {
self.transform = .init(scaleX: 0.9, y: 0.9)
}, completion: nil)
}
func touchOut(){
UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction,.curveEaseOut], animations: {
self.transform = .identity
}, completion: nil)
let impactFeedbackgenerator = UIImpactFeedbackGenerator(style: .rigid)
impactFeedbackgenerator.prepare()
impactFeedbackgenerator.impactOccurred()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
touchIn()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
touchOut()
}
}在我的主类中,我创建了这样的按钮:
let settingsbtn = CustomButton()
settingsbtn.backgroundColor = .clear
settingsbtn.layer.borderWidth = 0
settingsbtn.frame = CGRect(x: view.bounds.size.width - 60, y: 60, width: 50, height: 50)
settingsbtn.tintColor = UIColor.gray
settingsbtn.setImage(settingsImage, for: .normal)
settingsbtn.layer.zPosition = 2
settingsbtn.tag = 5
self.view.addSubview(settingsbtn)我该如何使用自定义按钮来定位新的视图控制器。我试过很多方法,但我似乎不能得到它。我会在UIButton类中将其创建为目标,但该类不能识别任何带有视图的内容。谢谢!
为了增加一些清晰度,我打算按下按钮并呈现一个不同的视图控制器。
发布于 2020-12-03 01:14:36
你有几个选择。
首先,不是覆盖touch事件,而是覆盖isHighlighted并在那里执行动画。然后,你可以使用普通的.addTarget(...),并在你的touchUpInside选择器函数中触发触觉反馈。
下面是一个完整的示例:
class CustomButton: UIButton {
override init(frame: CGRect){
super.init(frame: frame)
setConfig()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setConfig()
}
func setConfig(){
backgroundColor = UIColor(named: "SpecialGreen")
layer.borderColor = UIColor.darkGray.cgColor
layer.borderWidth = 1
layer.cornerRadius = 5.0
layer.zPosition = 2
titleLabel?.font = UIFont(name: "PingFangSC-Semibold", size: 15)
}
override var isHighlighted: Bool {
didSet {
if isHighlighted {
UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction,.curveEaseIn], animations: {
self.transform = .init(scaleX: 0.9, y: 0.9)
}, completion: nil)
} else {
UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction,.curveEaseOut], animations: {
self.transform = .identity
}, completion: nil)
}
}
}
}和示例视图控制器:
class CustViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let settingsbtn = CustomButton()
settingsbtn.backgroundColor = .clear
settingsbtn.layer.borderWidth = 0
settingsbtn.frame = CGRect(x: view.bounds.size.width - 60, y: 60, width: 50, height: 50)
settingsbtn.tintColor = UIColor.gray
if let settingsImage = UIImage(systemName: "slider.horizontal.3") {
settingsbtn.setImage(settingsImage, for: .normal)
}
settingsbtn.layer.zPosition = 2
settingsbtn.tag = 5
self.view.addSubview(settingsbtn)
settingsbtn.addTarget(self, action: #selector(self.settingsButtonTapped(_:)), for: .touchUpInside)
}
@objc func settingsButtonTapped(_ sender: UIButton) -> Void {
print("Settings Button Tapped")
// haptic feedback
let impactFeedbackgenerator = UIImpactFeedbackGenerator(style: .rigid)
impactFeedbackgenerator.prepare()
impactFeedbackgenerator.impactOccurred()
// do something, such as
let vc = SomeViewController()
present(vc, animated: true, completion: nil)
}
}如果你真的想把触觉反馈放在你的自定义按钮中,你可以在touchesEnded中发送touchUpInside事件。如果触摸在按钮内部结束,您可能只想触发触觉反馈和“轻敲”事件。
以下是采用该方法的完整示例:
class CustomButton: UIButton {
override init(frame: CGRect){
super.init(frame: frame)
setConfig()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setConfig()
}
func setConfig(){
backgroundColor = UIColor(named: "SpecialGreen")
layer.borderColor = UIColor.darkGray.cgColor
layer.borderWidth = 1
layer.cornerRadius = 5.0
layer.zPosition = 2
titleLabel?.font = UIFont(name: "PingFangSC-Semibold", size: 15)
}
func touchIn(){
UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction,.curveEaseIn], animations: {
self.transform = .init(scaleX: 0.9, y: 0.9)
}, completion: nil)
}
func touchOut(){
UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction,.curveEaseOut], animations: {
self.transform = .identity
}, completion: nil)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
touchIn()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
touchOut()
// only trigger haptic feedback and touchUpInside event
// if touches ended inside self
if let touch = touches.first {
let touchpoint:CGPoint = touch.location(in: self)
if bounds.contains(touchpoint) {
let impactFeedbackgenerator = UIImpactFeedbackGenerator(style: .rigid)
impactFeedbackgenerator.prepare()
impactFeedbackgenerator.impactOccurred()
self.sendActions(for: UIControl.Event.touchUpInside)
}
}
}
}和(几乎相同的)示例视图控制器:
class CustViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let settingsbtn = CustomButton()
settingsbtn.backgroundColor = .clear
settingsbtn.layer.borderWidth = 0
settingsbtn.frame = CGRect(x: view.bounds.size.width - 60, y: 60, width: 50, height: 50)
settingsbtn.tintColor = UIColor.gray
if let settingsImage = UIImage(systemName: "slider.horizontal.3") {
settingsbtn.setImage(settingsImage, for: .normal)
}
settingsbtn.layer.zPosition = 2
settingsbtn.tag = 5
self.view.addSubview(settingsbtn)
settingsbtn.addTarget(self, action: #selector(self.settingsButtonTapped), for: .touchUpInside)
}
@objc func settingsButtonTapped() -> Void {
print("Settings Button Tapped")
// do something, such as
let vc = SomeViewController()
present(vc, animated: true, completion: nil)
}
}https://stackoverflow.com/questions/64992244
复制相似问题