1
我創建了具有多個可移動(使用平移手勢識別器)UIImageView
對象的故事板,默認情況下它們是隱藏的。我有一個UIButton
,即當被按下時,產生隨機X以及按鈕Y位置被移動到如下:更改UILabel文本會導致可移動的UIImageView對象恢復到原始位置
// 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
的恢復到其默認位置爲SPECI在故事板上拼湊(加上任何轉換作爲平移的結果)。具體而言,該方法的最後一行導致問題:
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
文本時,從他們的隨機位置恢復到默認故事板的位置。
這很奇怪......所以你說當你刪除最後一行時,「timerLabel.text!=」Timer:...「,一切正常? – 2014-11-02 21:48:15
是的,如果我評論說它沒問題。 UIImageView不改變位置 – 2014-11-02 21:53:12
自動佈局正在運行並重新定位你的對象關閉自動佈局 – vacawama 2014-11-02 22:01:07