Swift NSTimer 和 IBOutlet 问题 [英] Swift NSTimer and IBOutlet Issue

查看:62
本文介绍了Swift NSTimer 和 IBOutlet 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NSTimer 更新图像在屏幕上的位置,以产生运动的错觉.这是我为了学习 Swift 而创建的一个蓬松的鸟克隆.无论如何,我还通过显示递增的 Int 作为分数来更新插座.但是,每次我更新 Int 和相应的出口时,屏幕上的图像都会重置为它们的起点(鸟和管).很奇怪.有没有其他人遇到过这个?我做错了什么?

I'm using NSTimer to update an image's position on the screen to give the illusion of movement. It's a flappy bird clone I'm creating to learn Swift. Anyway, I'm also updating an outlet by displaying an incrementing Int as the score. However, every time I update the Int and the corresponding outlet, the images on the screen reset to their starting point (bird and tubes). Very odd. Has anyone else come across this? What am I doing wrong?

@IBOutlet weak var tube1: UIImageView!
@IBOutlet weak var bird1: UIImageView!
@IBOutlet weak var score: UILabel!

func startTimer() {
    timer = NSTimer.scheduledTimerWithTimeInterval(0.02, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}

func update() {
    bird1.center=CGPointMake(50,bird1.center.y+CGFloat(num));
    tube1.center=CGPointMake(tube1.center.x-CGFloat(num2),tube1.center.y);
    if(tube1.center.x<(-40)){
        tube1.center.x=screenWidth;//variable set outside this method
        tube1.center.y=0;//will be random
        updateMyScore();
    }
}

func updateMyScore(){
    myScore=++;

    score.text="Score: \(myScore)"
}

奇怪的是,如果我注释掉 score.text="Score:(myScore)" 行,一切正常,但我想让分数显示在屏幕上.有什么建议吗?

The weird thing is if I comment out the score.text="Score:(myScore)" line everything works fine, but I'd like to have the score show on the screen. Any suggestions?

推荐答案

AutoLayout 正在运行并重置图像的位置.它正在运行,因为您正在更新您的 UILabel.有两种方法可以解决这个问题:

AutoLayout is running and resetting the position of your images. It is running because you are updating your UILabel. There are 2 ways you can deal with this:

  1. 禁用自动布局或
  2. 将管道的位置存储在属性中,然后在 viewDidLayoutSubviews() 的覆盖中设置它们.
  1. Disable AutoLayout OR
  2. Store the positions of your tubes and pipes in properties and then set them in an override of viewDidLayoutSubviews().

<小时>

以下是您可以如何实施选项 #2:


Here is how you might implement option #2:

@IBOutlet weak var tube1: UIImageView!
@IBOutlet weak var bird1: UIImageView!
@IBOutlet weak var score: UILabel!
var tube1center: CGPoint?
var bird1center: CGPoint?

func startTimer() {
    timer = NSTimer.scheduledTimerWithTimeInterval(0.02, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}

func update() {
    bird1.center=CGPointMake(50,bird1.center.y+CGFloat(num));
    tube1.center=CGPointMake(tube1.center.x-CGFloat(num2),tube1.center.y);
    if(tube1.center.x<(-40)){
        tube1.center.x=screenWidth;//variable set outside this method
        tube1.center.y=0;//will be random
        updateMyScore();
    }
    bird1center = bird1.center
    tube1center = tube1.center
}

func updateMyScore(){
    myScore=++;

    score.text="Score: \(myScore)"
}

override func viewDidLayoutSubviews() {
    if let newcenter = tube1center {
        tube1.center = newcenter
    }
    if let newcenter = bird1center {
        bird1.center = newcenter
    }
}

这篇关于Swift NSTimer 和 IBOutlet 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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