Swift:计时器重置视图 [英] Swift: timer resetting the view

查看:139
本文介绍了Swift:计时器重置视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个标签,每秒使用 NSTimer 计算1。我创建了一个具有一些移动 UIImage 的应用程序。但是,每次计时器计数一次时,视图似乎重新加载, UIImage s返回其原始位置。此外, UIImage 不是我把它们放在故事板中的地方(我将它们放在视图之外,这样它们就可以向内移动了,但是当我启动应用程序时它只显示他们已经在那里。他们移动但只有一秒然后回到原来的位置)。相同的代码在iPhone上工作正常,但在iPad上不起作用。我认为它与约束有关,因为代码是相同的。这是计时器代码:

So I have a label that counts 1 every second using an NSTimer. I made an app that has a few moving UIImages. However every time the timer counts one up, the view seemingly reloads and the UIImages go back to their original positions. Furthermore, the UIImages are not where I had placed them in the storyboard (I placed them outside the view so they could move inwards, but when I start the app it just shows them right there already. They move but only for one second then go back to their original positions). Same code works fine on the iPhone but doesn't work on an iPad. I think it has something to do with the constraints because the code is the same. Here's the timer code:

 func counting() {

    timerCount = timerCount + 1
    timerLabel.text = "\(timerCount)"


}


override func viewDidLoad() {

    super.viewDidLoad()

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("counting"), userInfo: nil, repeats: true)

    // Do any additional setup after loading the view, typically from a override nib.
}

以下是移动我的UIImages的代码:

Here's the code for moving my UIImages:

func MoveWalls() {


    FirstTimer = NSTimer.scheduledTimerWithTimeInterval(0.07, target: self, selector: Selector("FirstMoving"), userInfo: nil, repeats: true)

    SecondTimer = NSTimer.scheduledTimerWithTimeInterval(0.007, target: self, selector:
        Selector("SecondMoving"), userInfo: nil, repeats:true)

    ThirdTimer = NSTimer.scheduledTimerWithTimeInterval(0.006, target: self, selector:
        Selector("ThirdMoving"), userInfo: nil, repeats:true)

    FourthTimer = NSTimer.scheduledTimerWithTimeInterval(0.005, target: self, selector:
        Selector("FourthMoving"), userInfo: nil, repeats:true)
}

func FirstMoving() {
    First.center = CGPointMake(First.center.x + 1, First.center.y)
    }
func SecondMoving() {
    Second.center = CGPointMake(Second.center.x - 1, Second.center.y)
    }
func ThirdMoving() {
    ThirdMoving.center = CGPointMake(Third.center.x, Third.center.y + 1)
    }
func FourthMoving() {
    Fourth.center = CGPointMake(Fourth.center.x, Fourth.center.y  1)
    }

我的约束:

开始和结束游戏的两个按钮(水平居中)。

Two buttons that start and end the game (Centered horizontally).

四个UIImages(大小比率)

The four UIImages (size ratio)

一个计时器(左上角),由于某种原因,每次计数都会重置视图。

A timer (Top left) which for some reason resets the view with each count.

推荐答案

我遇到了完全相同的问题。我通过为每个可移动视图添加此代码来解决它:

I had the exact same problem. I solved it by adding this code for each of the moveable views:

override func viewDidLoad() {
    super.viewDidLoad()
    self.imageView.translatesAutoresizingMaskIntoConstraints = true
    self.imageView2.translatesAutoresizingMaskIntoConstraints = true
}

这篇关于Swift:计时器重置视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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