如何在Swift中按下按钮来遍历功能

时间:2019-01-23 04:12:58

标签: ios swift

我正在学习快速和构建ARApp,但似乎无法理解如何在每次单独按下按钮时迭代几个功能。我已经创建了3个内置动画的func,并且想要按一次按钮并激活funcAnimation#1,然后再次点击按钮以继续进行funcAnimation#2,依此类推。

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

当然,这里的问题是它们都一次激活。我只想在每次按下按钮时迭代一次按钮。另外,我还希望有一个backButton可以将当前动画反转为先前的动画。我在Apple文档中读到有一个addTarget方法,但我不了解它的工作方式或实现方式。请帮忙!

3 个答案:

答案 0 :(得分: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
    }
}

答案 1 :(得分: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 
    }
}

答案 2 :(得分:-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
    }