简体   繁体   English

按钮不改变位置

[英]Button not changing position

I have a homework assignment where we need to have a "timer" that counts how many times this dot moves (it runs on a timer but is an int). 我有一个家庭作业,我们需要有一个“计时器”来计算该点移动多少次(它在计时器上运行,但是一个整数)。 The source code my professor gave us works fine but when I add an updateTime() call to the moveButton() call it immediately stops working and only the timer will run instead, not both. 我的教授给我们的源代码工作正常,但是当我将updateTime()调用添加到moveButton()调用时,它立即停止工作,只有计时器将运行,而不是同时运行。

    func updateTimer (){
    value = value + 1;
    timerLabel.text = "Time: " + String(value);

    if (value >= 20){
        gameOver = true;
    }
}

    func moveButton(t:NSTimer){

    if (!gameOver && !paused){

        let aWidth = self.view.bounds.size.width;
        let aHeight = self.view.bounds.size.height;
        let btnX = random() % (Int)(aWidth-60);
        let btnY = random() % (Int)(aHeight-60);
        button.frame = CGRectMake(CGFloat(btnX), CGFloat(btnY), 30.0, 30.0);
        updateTimer();
    }



}

override func viewDidLoad() {
    super.viewDidLoad()
    srandom(arc4random());
    timer = NSTimer.scheduledTimerWithTimeInterval(1.5, target: self, selector: Selector("moveButton:"), userInfo: nil, repeats: true)

}

As you can see as the view loads it starts the timer. 您可以在视图加载时看到它启动计时器。 The timer calls moveButton(t:NSTimer) which eventually calls the updateTimer() method. 计时器调用moveButton(t:NSTimer),最终调用updateTimer()方法。 If you remove the updateTimer() method call it will work fine. 如果删除updateTimer()方法调用,它将正常工作。 So I tried removing 1 line at a time to see what was causing the problem and the problem is setting timerLabel.text to the time. 因此,我尝试一次删除1行,以查看引起问题的原因,并且问题是将timerLabel.text设置为该时间。 Why would that stop the dot from moving? 为什么要阻止点移动? Why can't it update the label text AND move the button? 为什么不能更新标签文本并移动按钮?

  1. Add Outlets to the layout constraints 将插座添加到布局约束

If you open your split view in XCode and then right click on a constraint on your button such as the top constraint, you can hold down right click and drag over to the controller for that button. 如果在XCode中打开拆分视图,然后右键单击按钮上的约束(例如顶部约束),则可以按住鼠标右键并拖动到该按钮的控制器上。 When you let go you will be able to create a constraint outlet for that button. 放开时,您将可以为该按钮创建约束出口。

From here you can add or subtract from that constraint to move the button using: 您可以在此处从该约束中添加或减去以使用以下方法移动按钮:

constraintTop.constant = x
constraintLeft.constant = y

Likely you are using auto-layout (ie you have constraints defined in IB that define the location of the button). 可能您正在使用自动布局(即,您在IB中定义了约束来定义按钮的位置)。 The problem is that when you update the text of the label, auto-layout constraints are automatically reapplied, and thus your attempt to adjust the button frame is being thwarted. 问题在于,当您更新标签的文本时,会自动重新应用自动布局约束,因此,调整按钮frame尝试受到了阻碍。

There are a bunch of ways of solving this. 有很多解决方法。

  1. The best way, IMHO, is to create outlets for the top and leading constraints of the button. 最好的办法,恕我直言,是创造网点topleading按钮的约束。 Then you can programmatically adjust the constant properties for those two constraints using the outlets you have created. 然后,您可以使用创建的出口以编程方式调整这两个约束的constant属性。

  2. There are less elegant approaches that bypass auto-layout, including: 绕过自动布局的优雅方法较少,包括:

    • set up the button so it has no constraints (eg edit the constraints in IB and click on the "Remove at build time" option; 设置按钮使其不受约束(例如,在IB中编辑约束,然后单击“在构建时删除”选项;

    • programmatically removing the constraints associated with the button; 以编程方式删除与按钮关联的约束; or 要么

    • turning off auto-layout. 关闭自动版式。

    Then you can adjust the frame of the button without fear of the auto-layout engine moving it back to the original position when the label is updated. 然后,您可以调整按钮的frame ,而不必担心标签更新时自动布局引擎会将其移回到原始位置。

But, I'd lean towards adding outlets for the constraints, and then you can have the autolayout engine move the button for you. 但是,我倾向于添加约束的插座,然后可以让自动布局引擎为您移动按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM