ios - 如何在 Swift 中通过按下按钮来遍历函数

标签 ios swift

我正在学习 swift 并构建 ARApp,但似乎无法理解如何在每次按下单独的按钮时迭代几个函数。我已经创建了 3 个内置动画的功能,我想按下按钮一次并激活 funcAnimation#1,然后再次点击按钮继续 funcAnimation#2 等等。

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

当然这里的问题是它们都同时激活。我只想对每次按下的按钮进行迭代。此外,我还希望有一个 backButton 将当前动画反转为上一个动画。我在 Apple 的文档中读到有一个 addTarget 方法,但我不明白它是如何工作的或如何实现它。请帮忙!

最佳答案

你的代码应该是这样的:

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

你的后退 Action 应该是这样的:

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

关于ios - 如何在 Swift 中通过按下按钮来遍历函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54319919/

相关文章:

ios - iOS版Flutter ImageCrop库相机权限错误

iOS:如何从一个应用程序打开其他已安装的应用程序?

ios - 如何在 iPhone、iOS 8 上为弹出窗口设置动画

ios - 在方向变化时以编程方式重绘 subview

ios - 长宽比限制没有改变

ios - 如何在tableview中显示大量数据

ios - 为自定义类设置委托(delegate)对象

ios - 如何从 UIActivityItemSource 函数返回多个值

ios - UIView 子类不能使用 UIView 自定义类?

iphone - 使图像适合屏幕