如何解决如何分解包括选择器的功能?
我有几个具有相同作用的视图。我试图分解该函数,但是没有用。 这是ViewController中代码的一部分:
class MenuViewController: UIViewController {
@IBOutlet weak var addButton: FloatingActionButton!
@IBOutlet weak var viewCALORIES: UIView!
@IBOutlet weak var viewPROTEInes: UIView!
@IBOutlet weak var viewLIPIDES: UIView!
@IBOutlet weak var viewgluCIDES: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// BoingCalories Protéines Lipides glucides
let tapCalories = UITapGestureRecognizer(target: self,action: #selector(boingCALORIES(gesture:)))
viewCALORIES.addGestureRecognizer(tapCalories)
let tapProteines = UITapGestureRecognizer(target: self,action: #selector(boingPROTEInes(gesture:)))
viewPROTEInes.addGestureRecognizer(tapProteines)
let tapLipides = UITapGestureRecognizer(target: self,action: #selector(boingLIPIDES(gesture:)))
viewLIPIDES.addGestureRecognizer(tapLipides)
let tapglucides = UITapGestureRecognizer(target: self,action: #selector(boinggluCIDES(gesture:)))
viewgluCIDES.addGestureRecognizer(tapglucides)
}
@objc private func boingCALORIES(gesture: UITapGestureRecognizer) {
viewCALORIES.transform = CGAffineTransform(scaleX: 0.5,y: 0.5)
UIView.animate(withDuration: 0.6,delay: 0,usingSpringWithdamping: 0.3,initialSpringVeLocity: 0.4,options: [],animations: { self.viewCALORIES.transform = .identity },completion: nil)
}
@objc private func boingPROTEInes(gesture: UITapGestureRecognizer) {
viewPROTEInes.transform = CGAffineTransform(scaleX: 0.5,animations: { self.viewPROTEInes.transform = .identity },completion: nil)
}
@objc private func boingLIPIDES(gesture: UITapGestureRecognizer) {
viewLIPIDES.transform = CGAffineTransform(scaleX: 0.5,animations: { self.viewLIPIDES.transform = .identity },completion: nil)
}
@objc private func boinggluCIDES(gesture: UITapGestureRecognizer) {
viewgluCIDES.transform = CGAffineTransform(scaleX: 0.5,animations: { self.viewgluCIDES.transform = .identity },completion: nil)
}
我尝试了一些测试,但是失败了。那么我如何将所有这些因素分解呢? 谢谢
解决方法
您可以从UITapGestureRecognizer
中检索视图。
let view = gesture.view
因此您可以这样做:
@objc private func boingView(gesture: UITapGestureRecognizer) {
let view = gesture.view
view.transform = CGAffineTransform(scaleX: 0.5,y: 0.5)
UIView.animate(withDuration: 0.6,delay: 0,usingSpringWithDamping: 0.3,initialSpringVelocity: 0.4,options: [],animations: { view.transform = .identity },completion: nil)
}
并进行通话分解:
let action = #selector(boingView(gesture:))
let tapCalories = UITapGestureRecognizer(target: self,action: action)
viewCALORIES.addGestureRecognizer(tapCalories)
let tapProteines = UITapGestureRecognizer(target: self,action: action)
viewPROTEINES.addGestureRecognizer(tapProteines)
let tapLipides = UITapGestureRecognizer(target: self,action: action)
viewLIPIDES.addGestureRecognizer(tapLipides)
let tapGlucides = UITapGestureRecognizer(target: self,action: action)
viewGLUCIDES.addGestureRecognizer(tapGlucides)
如果走得更远,我们可以将视图放入数组并在它们上循环:
let views = [viewCALORIES,viewPROTEINES,viewLIPIDES,viewGLUCIDES]
let action = #selector(boingView(gesture:))
views.forEach { aView in
let tap = UITapGestureRecognizer(target: self,action: action)
aView.addGestureRecognizer(tap)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。