如何在Swift中通过按钮按下迭代函数

8

我正在学习Swift并构建一个AR应用程序,但似乎无法理解如何在每次单独的按钮按下时迭代通过几个函数。我已经创建了3个带有动画的函数,并希望按一次按钮来激活funcAnimation#1,然后再次点击按钮以继续进行funcAnimation#2等。

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

当然,这里的问题是它们都同时激活。我想在每次按下按钮时只迭代一次。此外,我还想有一个返回按钮,可以将当前动画反转到上一个动画。我在苹果的文档中读到了有一个addTarget方法,但我不知道它是如何工作或如何实现它。请帮忙!


使用一个计数器(整数)参数,在每次按钮点击时递增。添加一个条件,当达到限制时(即3),将该计数器设置为零。 - Bhavik Modi
1
你也可以使用完成处理程序。一旦函数运行结束,便会运行下一个函数。 - cyril
3个回答

3
你的代码应该像这样:

你的代码应该像这样:

// 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
    }
}

你的“返回”操作应该像这样:

您的返回操作应该是这个样子:

@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
    }
}

你可以在点击按钮后禁用它,以完成第一个动画,然后是第二个,第三个也是如此。如何处理用户界面流程完全取决于你。 - Jarvis The Avenger
谢谢你,贾维斯。这个做得很好。每次按下按钮时动画会单独发生。我还有一个快速的问题。我有两个单独的按钮,一个是“下一个”,应用动画,另一个是“返回”。有没有一种方法可以将当前动画状态反转为上一个动画状态呢?例如,如果您在第三个动画上,按下“返回”按钮时它会自动回到前一个动画状态(第二个状态)吗? - Sam
是的,您可以通过检查后退按钮来实现这一点。在其中将计数器减1并播放动画。但是有一个破坏性条件,如果计数器等于0,则不要减少计数器。将其重置为2。 - Jarvis The Avenger
@Sam,我已经更新了你的返回按钮操作的答案。 - Jarvis The Avenger
返回操作确实会注册我按下返回按钮的动作,并且理论上应用了该更改,但它并没有撤消动画。由于在按下“下一个”按钮时已经发生了动画,因此您看不到任何东西,但是您的答案已在控制台中注册。我正在使用SCNTransaction进行动画,但似乎没有.reversed方法。我将继续研究,但我认为这是另一个话题。我不想浪费您的时间。您的答案确实有所帮助并且非常准确。谢谢! - Sam
@Sam 谢谢,你可以尝试使用 UIViewPropertyAnimator 来控制你的动画。 - Jarvis The Avenger

1

另一种方法!

只需设置按钮1的标签(例如btnObj.tag=1

并在按钮的操作中管理方法,例如

我的建议还是要管理flag以进行动画控制,例如如果第一个动画正在进行中,则第二个动画将等待完成后才能点击按钮,这样您的动画就不会出现错误。

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 
    }
}

-1

您可以通过类似于此的返回按钮,将动画转发并返回到先前的动画状态。

步骤1:声明两个整数类型变量

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

步骤2:在您的IBAction函数中

    @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
        }
    }

步骤3:在您的函数中

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

步骤4:最后,用于从当前状态反转动画

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

    }
    else if currentAnimation == 3
    {
        Animation2()

    }
    else
    {

    }
    tapCount = 0
}

希望能有所帮助!!
对于 addTarget(_:action:for:) 方法,可以在不连接 IBAction 并在需要时在 viewWillAppear 或 viewDidLoad 中声明的情况下完成。它将把目标对象和操作方法与控件关联起来。
例如:
    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
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接