【问题标题】:Swift: How to trigger functions in Xcode projects?Swift:如何在 Xcode 项目中触发函数?
【发布时间】:2015-10-13 04:19:58
【问题描述】:

我是一个编程新手,我不知道如何在我的项目中触发我的函数。我只需要我的函数运行 4 秒,然后它就不必再次工作了。我不想要一个按钮来调用该功能,我希望它是自动的!谢谢!

PS。定时器变量是上面运行的定时器。

func fadeLabels() {
    if timerCount > 3 {
        timer.invalidate()
        timerRunning = false
    }

    //Label Fade Functionality
    if timerCount == 1 {
        UIView.animateWithDuration(1.0, animations: { () -> Void in
            self.countdownThree.alpha = 0
        })
    }

    if timerCount == 2 {
        UIView.animateWithDuration(1.0, animations: { () -> Void in
            self.countdownTwo.alpha = 0
        })
    }
    if timerCount == 3 {
        UIView.animateWithDuration(1.0, animations: { () -> Void in
            self.countdownOne.alpha = 0
        })
    }
}

感谢任何帮助我解决愚蠢问题的人!

【问题讨论】:

标签: swift function timer triggers call


【解决方案1】:

在控制器的viewDidLoad方法中添加代码:

NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: "fadeLabels", userInfo: nil, repeats: false)

【讨论】:

  • 我实际上将该代码添加到 viewDidLoad 并且只有最后一个 if 语句有效。有什么解决办法吗?
  • F.e.使用不同的超时时间(第一个参数)调用 scheduleTimerWithTimeInterval 函数 3 次
  • 很高兴能为您提供帮助。请检查我的回答是否“已接受”
猜你喜欢
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 2012-06-24
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-30
相关资源
最近更新 更多