2016-05-19 2 views
0

У меня есть 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") 
} 
+0

Эти жесткокодированные значения '.One' ...' .Nine' действительно должны быть индексами 'Int' в массиве – Alexander

+0

Я не уверен, как это сделать в перечислении. У вас есть пример? – aBikis

ответ

0

Вы можете сделать одну функцию (или даже расширение), которая принимает пару параметров для задания задачи. Затем вместо того, чтобы записывать целую функцию для каждого из этих распознавателей, вы можете просто вызвать ее и указать параметры для задачи.

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 распознавателях соответствующего значения Int и имеет все они отвечают на этот селектор.

+0

получил. Я закончил подклассификацию UIButton вместо UIView, чтобы установить триггеры анимации, а затем установить тег в UIButton. благодаря! – aBikis

Смежные вопросы