Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

297
Visualizações
how to iterate through functions with button press in Swift

I'm learning swift and building an ARApp but can't seem to understand how to iterate through a few functions with every separate button press. I have created 3 func with animation built in and want to press the button once and funcAnimation#1 activate then tap button again to proceed to funcAnimation#2 and so forth.

@IBAction func nextAnimation(_ sender: UIButton) {
funcAnimation#1()
funcAnimation#2()
funcAnimation#3()
}

of course the problem here is that they all activate at once. I would like to iterate only on button press for each individual press. In addition I would also like to have a backButton that reverses the current animation to previous animation. I read in Apple's documentation that there is a addTarget method but I don't understand how it works or how to implement it. Please Help!

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Your code should like this :

// You can define this variable globally...
var counter = 0
@IBAction func nextAnimation(_ sender: UIButton) {
    if (counter == 0) {
        funcAnimation1()
        // Increase counter count by 1 and you can add this line to completion of animation.
       // You can also disable your button interaction until your animation gets complete and that way you can handle your UI
        count += 1
    }
    else if (counter == 1) {
        funcAnimation2()
         // Increase counter count by 1 and you can add this line to completion of animation.
        count += 1
    }
    else if (counter == 2) {
        funcAnimation3()
        // set your counter to 0 again to loop through your animation.
        counter = 0
    }
}

Your Back Action should look like this:

@IBAction func backAnimation(_ sender: UIButton) {
    if (counter == 0) {
        funcAnimation1()
        // set your counter to 2 again to loop through your animation.
        count = 2
    }
    else if (counter == 1) {
        funcAnimation2()
        // decrease counter count by 1 and you can add this line to completion of animation.
        // You can also disable your button interaction until your animation gets complete and that way you can handle your UI
        count -= 1
    }
    else if (counter == 2) {
        funcAnimation3()
       // decrease counter count by 1 and you can add this line to completion of animation.
        count -= 1
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

Another way!

Just set tag of button 1 (Ex. btnObj.tag=1)

And manage method(s) in action of the button like

My suggestion is also manage flag for animation like if 1st animation is in process then 2nd will be waiting for finish and after finishing 1st animation, button will be clicked so your animation will not mismatch.

var isAnimated = false;
@IBAction func myAnimationMethod(_ sender: UIButton) {
    if isAnimated {return} // Or display appropriate message by alert
    if (sender.tag == 1) {
        isAnimated=true
        funcAnimation1(...Your Code... After finish to set isAnimated=false)
        sender.tag = 2 
    }
    else if (sender.tag  == 2) {
       isAnimated=true
        funcAnimation2(...Your Code... After finish to set isAnimated=false)
        sender.tag = 3 
    }
    else if (sender.tag  == 3) {
         isAnimated=true
        funcAnimation3(...Your Code... After finish to set isAnimated=false)
        sender.tag = 1 
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

You can forward your animations and go back to previous animation state through a back button like this .

Step 1 : Declare two integer type variables

var tapCount = 0 //For forwarding your animations from first to third
var currentAnimation = 0 // For reversing the animation from current animation

Step 2 : In your IBAction Function

    @IBAction func nextAnimation(_ sender: Any)
    {
        if tapCount == 0
        {
            if currentAnimation == 1
            {
                Animation2()
            }
            else
            {
                Animation1()
            }
            tapCount += 1

        }
        else if tapCount == 1
        {
            if currentAnimation == 2
            {
                Animation3()
            }
            else
            {
                Animation2()
            }
            tapCount += 1
        }
        else if tapCount == 2
        {
            if currentAnimation == 3
            {
                Animation1()
            }
            else
            {
                Animation3()
            }
            tapCount = 0
        }
    }

Step 3 : In your functions

    func Animation1()  {
        currentAnimation = 1
       print("First Animation")
    }
    func Animation2()  {
        currentAnimation = 2
         print("Second Animation")
    }
    func Animation3()  {
        currentAnimation = 3
     print("third Animation")
    }

Step 4: Lastly For Reversing the animation from current state

@IBAction func backAnimation(_ sender: Any)
{
    if currentAnimation == 2
    {
         Animation1()

    }
    else if currentAnimation == 3
    {
        Animation2()

    }
    else
    {

    }
    tapCount = 0
}

Hope it helps !!

For addTarget(_:action:for:) method , this can be done without connecting a IBAction and declaring it in viewWillAppear or viewDidLoad as required.It will Associates a target object and action method with the control.

For example :

    override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            nextButton.addTarget(self, action: #selector(self. nextAnimation), for: .touchUpInside) //Declared outlet as nextButton
            backButton.addTarget(self, action: #selector(self. backAnimation), for: .touchUpInside) //Declared outlet as backButton and this adds a target
        }
    @objc func nextAnimation(sender: UIButton) {
       // Do your stuff for next animation
    }
   @objc func backAnimation(sender: UIButton) {
       // Do your stuff for previous animation
    }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda