ios - 如何等到动画在 Swift 中完成?

标签 ios swift animation nstimer

在我的测验的下一个问题加载之前,我尝试让按钮“摇动”。

动画看起来像

var timer = NSTimer()
    UIView.animateWithDuration(0.1, animations: {
        self.IMGVIEWcat.center = CGPointMake(self.IMGVIEWcat.center.x + 2, self.IMGVIEWcat.center.y)
    })
    UIView.animateWithDuration(0.2, animations: {
        self.IMGVIEWcat.center = CGPointMake(self.IMGVIEWcat.center.x - 4, self.IMGVIEWcat.center.y)
    })
    UIView.animateWithDuration(0.3, animations: {
            self.IMGVIEWcat.center = CGPointMake(self.IMGVIEWcat.center.x + 2, self.IMGVIEWcat.center.y)
    })

之后调用下面的函数

    func QueryInformations(){
        println("QueryInformationsStart")
        self.ActivityIndicator.hidden = false
        ObjectIDsArrayCount = ObjectIDsArray.count
        var RandomQuestion = Int(arc4random_uniform(ObjectIDsArray.count + 0))
        var QuestionID:String = ObjectIDsArray[RandomQuestion]
        var query : PFQuery = PFQuery(className: "AddonQuiz")
        query.getObjectInBackgroundWithId(QuestionID){
            (ObjectHolder : PFObject!, error : NSError!) -> Void in
...

现在动画开始但下一个问题立即加载,是否有一些简单的等待初学者可以实现“等待”直到动画完成?

我试着设置

var complet == true 

动画中

并在查询函数中

if complet = true {....

但这对我不起作用,我还找到了一些有关完成处理程序的信息,但没有在我的代码中使用它。

最佳答案

如果你想在动画结束时使用 completion block ,你应该使用带有 delayoptions 参数的结构作为出色地。

UIView.animateWithDuration(0.3, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { 
// put here the code you would like to animate
    self.IMGVIEWcat.center = CGPointMake(self.IMGVIEWcat.center.x + 2, self.IMGVIEWcat.center.y)

}, completion: {(finished:Bool) in
// the code you put here will be compiled once the animation finishes
    QueryInformations() 
})

关于ios - 如何等到动画在 Swift 中完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28441152/

相关文章:

ios - 在 UIAlertView 中重新定位 Activity Indicator

ios - 加载仅在按钮功能后显示的动画(Swift)

javascript - 当元素出现在屏幕上时如何启动动画?

ios - Flutter Podfile 和 Firebase

iphone - 设置动画总是出现

swift - 保存到 Today Extension 中的特定核心数据对象

swift - 如何使用swift将json的数组转换成数组

javascript - 带有 FormGroup 的 Angular 4 动画部分不起作用

ios - 除了 `UserDefaults` 之外,还有其他方法可以在 `Today Widget Extension` 和 iOS 中的主要 `Container Application` 之间共享数据吗

ios - iOS 3.x 上的 setShadowOffset 崩溃