斯威夫特 - 何时或如何实现对视图动画的开始? [英] Swift - when or how to implement animations on view start?

查看:96
本文介绍了斯威夫特 - 何时或如何实现对视图动画的开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的斯威夫特,我想实现一个简单的游戏。在这个游戏中,一旦视图被载入我想定期动画发生。

I'm new to Swift and I'm trying to implement a simple game. In this game, once the view is loaded I want periodic animations to happen.

问题是,我尝试动画我用的按钮,例如, button.frame.origin.x + = 50 ,但而不是从原点移动它以50像素的权利,它出现在button.frame.origin.x - 50进入其(初始位置)。
有趣的是,在一个点上我表现出 AlertDialog 给用户,并显示动画后开始如我所料的发生。

The problem is that I try to animate my buttons with, for instance, button.frame.origin.x += 50, but instead of moving it from the origin to 50px right, it appears at button.frame.origin.x - 50 and goes to its (initial) position. Funnily enough, at one point I show an AlertDialog to the user, and after it is shown the animation starts to happen as I expected.

我的问题是一样的这个话题,但接受的答案就是没有解决这个问题对我来说。

My problem is the same of this topic, but the accepted answer just didn't solve it for me.

有什么想法?

编辑:
挖掘到code和测试了很多,我发现,我显示AlertDialog也恰好我的无效计时器方法。我有两个定时器:一个更新UI时间(文本字段),另一个执行动画。两者都调度任务。从什么<一个href=\"https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/index.html\"相对=nofollow>我读到这里,它是不可能有两个定时器这样。

Digging into the code and testing a lot I found out the method where I show the AlertDialog also happens to invalidate my timer. I have two timers: one to update the UI time (a TextField), and the other to perform the animation. Both are scheduling a task. From what I read here, it is not possible to have two timers like that.

一个计时器对象只能在一个运行循环,一次注册,
  虽然它可以被添加到多个运行循环模式,运行中
  循环。

A timer object can be registered in only one run loop at a time, although it can be added to multiple run loop modes within that run loop.

因此​​,显而易见的解决方案是将两个选择器(功能)合并成一个,这样的计时器将工作两个任务。但是,如果我尝试更新UI并执行动画,动画并不如预期不再工作。我只是失去了在这个问题上,不能使双方工作的事情。

Hence, the obvious solution would be to merge the two selectors (functions) into one, so the timer would work for both tasks. However, if I try to update the UI AND perform animations, the animations don't work as expected anymore. I'm just lost on this problem and can't make both things work.

这是我的code的重要棋子:

These are my important pieces of code:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    ...

    resetOrCreateGame()
}

func resetOrCreateGame() {
    // Do stuff to initialize values
    timerToRotate = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: "rotateBlocks", userInfo: nil, repeats: true)
}

func rotateBlocks() {
    // Calculate positions to rotate
    UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
        // Try to update a timer in the UI
        //self.timeLeft.text = String(self.seconds)

        for i in 0 ... 8 {
            println("\(i) before \(self.buttons[i].frame.origin.x) \(self.buttons[i].frame.origin.y)")
            self.buttons[i].frame.origin.x = self.positions[self.indicesToRotate[i]].x
            self.buttons[i].frame.origin.y = self.positions[self.indicesToRotate[i]].y

            println("\(i) after \(self.buttons[i].frame.origin.x) \(self.buttons[i].frame.origin.y)")
        }
        }, completion: { _ in
                println("completed")
            })

如果我离开行 self.timeLeft.text =字符串(self.seconds)评论说,动画做工精细。无论我怎样尝试更新的timeleft,如果我那样做,我的螺丝钉的动画。我试图更新它在一个单独的线程,其分派到主线程,甚至更新的动画闭包:这是行不通的。

If I leave the line self.timeLeft.text = String(self.seconds) commented, the animations work fine. No matter how I try to update the timeLeft, if I do so it screws my animations. I tried to update it in a separate thread, dispatch it to the main thread, or even update it inside the animations closure: it just doesn't work.

推荐答案

最后,我找到了解决办法!
由于某种原因,这个问题是自动布局。禁用后一切都如预期。

Finally, I found the solution! The problem for some reason was Auto Layout. After disabling it everything worked as expected.

希望它可以帮助别人的未来!

Hope it helps someone in the future!

这篇关于斯威夫特 - 何时或如何实现对视图动画的开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆