如何分解包含选择器的函数?

Posted

技术标签:

【中文标题】如何分解包含选择器的函数?【英文标题】:how to factorize a function including a selector? 【发布时间】:2020-09-01 08:07:33 【问题描述】:

我有几个具有相同操作的视图。我试图分解函数,但它没有用。 这是 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, y: 0.5)
        UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.4, options: [], animations:  self.viewPROTEINES.transform = .identity , completion: nil)
    
    
    @objc private func boingLIPIDES(gesture: UITapGestureRecognizer) 
        viewLIPIDES.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
        UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.4, options: [], animations:  self.viewLIPIDES.transform = .identity , completion: nil)
    
    
    @objc private func boingGLUCIDES(gesture: UITapGestureRecognizer) 
        viewGLUCIDES.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
        UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.4, options: [], animations:  self.viewGLUCIDES.transform = .identity , completion: nil)
    

我尝试了一些测试,但都失败了。那么我怎么能分解所有这些呢? 谢谢

【问题讨论】:

您仍然可以将 2 行提取到一个函数中,然后从每个当前函数中使用正确的视图调用它,即使增益没有那么大 您可以从手势中检索视图。这就是您可能缺少的信息。 Larme,我不明白你从手势中检索视图是什么意思。请给我看看好吗? 【参考方案1】:

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)

【讨论】:

哪一部分在viewDidLoad里面,哪一部分在外面? 同样的逻辑:你应用addGesture()的地方,就是viewDidLoad()。另一个boingView,是另一种方法。

以上是关于如何分解包含选择器的函数?的主要内容,如果未能解决你的问题,请参考以下文章

如何计算选择器的权重

包含类、子选择器和相邻兄弟选择器的 CSS 选择器的等效 XPath 是啥?

如何将 jQuery 选择器的表达式作为文本获取?

jquery内容选择器(匹配包含指定选择器的元素)

包含子选择器的jQuery查找

单击 ImageView 并在 Swift/Xcode 中执行没有选择器的函数