编写一组执行类似操作的点击手势识别器的最雄辩的方式



我有 9 个视图和 9 个点击手势识别器,它们都执行非常相似的任务。我一直在绞尽脑汁想出一种更有说服力的写法,但什么也想不出来。任何帮助将不胜感激!

func flipToBack(){
    let matrix = Animations.HotSpotNumber(rawValue: currentHotSpot)
    guard let dict = matrix?.instructions else {return}
    flipLayerSecondPhaseWithInstructions(dict, layer: frontView.layer, directionOfTransformation: "towardsBackSide")
}
func flipToFront(){
    let matrix = Animations.HotSpotNumber(rawValue: currentHotSpot)
    guard let dict = matrix?.instructions else {return}
    flipLayerSecondPhaseWithInstructions(dict, layer: frontView.layer, directionOfTransformation: "towardsFrontSide")
}

func handleBackSideTap(){
    let animation = Animations.HotSpotNumber(rawValue: currentHotSpot)
    guard let dict = animation?.instructions else {return}
    flipLayerFirstPhaseWithInstructions(dict, layer: frontView.layer, directionOfTransformation: "towardsFrontSide")
}
func handleHotSpotOneTap(){
    let dict = Animations.HotSpotNumber.One.instructions
    currentHotSpot = 1
    flipLayerFirstPhaseWithInstructions(dict, layer: frontView.layer, directionOfTransformation: "towardsBackSide")
}
func handleHotSpotTwoTap(){
    let dict = Animations.HotSpotNumber.Two.instructions
    currentHotSpot = 2
    flipLayerFirstPhaseWithInstructions(dict, layer: frontView.layer, directionOfTransformation: "towardsBackSide")
}
func handleHotSpotThreeTap(){
    let dict = Animations.HotSpotNumber.Three.instructions
    currentHotSpot = 3
    flipLayerFirstPhaseWithInstructions(dict, layer: frontView.layer, directionOfTransformation: "towardsBackSide")
}
func handleHotSpotFourTap(){
    let dict = Animations.HotSpotNumber.Four.instructions
    currentHotSpot = 4
    flipLayerFirstPhaseWithInstructions(dict, layer: frontView.layer, directionOfTransformation: "towardsBackSide")
}
func handleHotSpotSixTap(){
    let dict = Animations.HotSpotNumber.Six.instructions
    currentHotSpot = 6
    flipLayerFirstPhaseWithInstructions(dict, layer: frontView.layer, directionOfTransformation: "towardsBackSide")
}
func handleHotSpotSevenTap(){
    let dict = Animations.HotSpotNumber.Seven.instructions
    currentHotSpot = 7
    flipLayerFirstPhaseWithInstructions(dict, layer: frontView.layer, directionOfTransformation: "towardsBackSide")
}
func handleHotSpotEightTap(){
    let dict = Animations.HotSpotNumber.Eight.instructions
    currentHotSpot = 8
    flipLayerFirstPhaseWithInstructions(dict, layer: frontView.layer, directionOfTransformation: "towardsBackSide")
}
func handleHotSpotNineTap(){
    let dict = Animations.HotSpotNumber.Nine.instructions
    currentHotSpot = 9
    flipLayerFirstPhaseWithInstructions(dict, layer: frontView.layer, directionOfTransformation: "towardsBackSide")
}

您可以创建一个接受几个参数来指定任务的函数(甚至扩展)。然后,无需为每个识别器编写整个函数,只需调用一个并指定任务的参数即可。

不知道你的Animations.HotSpotNumber.Four.instructions来源是什么样子的,很难说出最好的方法,但你可以做如下的事情。

var arrayOfInstructions: [[String: String]] = [["first": "inst"],["second":"inst"]]
func animateForTap(sender: UIGestureRecognizer) {
    let instructions = arrayOfInstructions[sender.tag]
    flipLayerFirstPhaseWithInstructions(instructions, layer: frontView.layer, directionOfTransformation: "towardsBackSide")

然后,可以将 Tap 识别器上的 Tag 属性设置为适当的 int 值,并让所有识别器都响应此选择器。

最新更新