gpt4 book ai didi

ios - 更改 UILabel 文本会导致可移动的 UIImageView 对象恢复到原始位置

转载 作者:行者123 更新时间:2023-11-28 05:33:13 25 4
gpt4 key购买 nike

我创建了一个包含多个可移动(使用平移手势识别器)UIImageView 对象的 Storyboard,这些对象默认隐藏。我有一个 UIButton,当按下它时,它会为要移动到的按钮生成随机的 X 和 Y 位置,如下所示:

// Places each puzzle piece at a random location on the screen
for puzzlePiece in puzzlePieces {

// Generate a random X position for the new center point of the puzzle,
// so that the piece is on the screen. Must convert to UInt and then CGFloat
var randomXPosition: CGFloat = CGFloat(UInt(114 + arc4random_uniform(796)))

// Generate a random Y position for the new center point of the puzzle,
// so that the piece is on the screen. Must convert to UInt and then CGFloat.
var randomYPosition: CGFloat = CGFloat(UInt(94 + arc4random_uniform(674)))

puzzlePiece.frame = CGRect(x: randomXPosition, y: randomYPosition, width: puzzlePiece.frame.width, height: puzzlePiece.frame.height)
}

UIImageViews 移动到随机位置后,它们将取消隐藏,显示计时器的 UILabel 开始跟踪时间,如下所示:

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

问题是每当 NSTimer 调用“updateTimerLabel”方法并且 UILabel 的 文本被修改时,所有的 UIImageViews 恢复到 Storyboard 上指定的默认位置(加上平移导致的任何转换)。具体来说,此方法的最后一行导致了问题:

func updateTimerLabel() {

secondsElapsed++;

var numSecondsToDisplay = secondsElapsed % 60
var numMinutesToDisplay = ((secondsElapsed - numSecondsToDisplay) % 3600) / 60
var numHoursToDisplay = (secondsElapsed - numSecondsToDisplay - numMinutesToDisplay) / 3600

var secondsToDisplay = String(format: "%02d", numSecondsToDisplay)
var minutesToDisplay = String(format: "%02d", numMinutesToDisplay)
var hoursToDisplay = String(format: "%02d", numHoursToDisplay)

timerLabel.text! = "Timer: \(hoursToDisplay):\(minutesToDisplay):\(secondsToDisplay)"
}

我想知道是否有任何方法可以防止 UIImageViews 在更改 UILabel 的 文本时从它们的随机位置恢复到它们的默认 Storyboard 位置。

最佳答案

自动布局正在运行并重新定位您的对象。关闭自动布局,您的对象将保留在您放置它们的位置。

关于ios - 更改 UILabel 文本会导致可移动的 UIImageView 对象恢复到原始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26704727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com