写出一组做类似事情的轻拍手势识别器的最有说服力的方式

时间:2016-05-19 19:02:31

标签: ios swift function uitapgesturerecognizer

我有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")
}

2 个答案:

答案 0 :(得分:0)

您可以创建一个接受一些参数来指定任务的单个函数(甚至是扩展名)。然后,您可以只调用一个并指定任务的参数,而不是为每个识别器写出整个函数。

答案 1 :(得分:0)

在不知道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值,并让它们全部响应这个选择器。