我创建了一个情节提要,其中包含多个可移动(使用平移手势识别器)UIImageView
对象,这些对象默认情况下处于隐藏状态。我有一个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's
修改了文本,所有内容都将UIImageViews
还原到情节提要上指定的默认位置(以及平移导致的所有转换)。具体来说,此方法的最后一行会导致此问题:
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's
文本时将其从随机位置恢复为默认情节提要板位置。
自动版式正在运行并重新定位对象。关闭自动版式,您的对象将保留在放置它们的位置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句