如何分解包括选择器的功能?

问题描述

我有几个具有相同作用的视图。我试图分解该函数,但是没有用。 这是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)
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...