I have seen 1 other question like this but I didn't help. In my application, I want a timer to start counting when I press the start button. I also want to move a button from one side of the screen to another (when I click the start button). To add to that, I want another button to move from one side of the screen to another after a 1 second delay of clicking the start button. The problem is that even though I gave a different name for each timer (NS Timer), they are messing around with each other. After clicking the start button, the timer for counting the seconds and the first button who moves work fine, but after 1 second, the first button goes back to the beginning and starts over and the second button starts to move but then it does the same thing as the first button. The timer that counts the seconds still works fine though. Heres the code (BTW I use CADisplayLink to move the buttons):
var displayLink: CADisplayLink?
var displayLink1: CADisplayLink?
@IBOutlet var moving1outlet: UIButton!
@IBOutlet var moving2outlet: UIButton!
@IBOutlet var timerlabel: UILabel!
var timer = NSTimer()
var timer2 = NSTimer()
var startTime = NSTimeInterval()
@IBAction func startbutton(sender: UIButton) {
timer.invalidate()
timer2.invalidate()
//CREATING THE COUNTING TIMER
let aSelector : Selector = "updateTime"
timer2 = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: aSelector, userInfo: nil, repeats: true)
startTime = NSDate.timeIntervalSinceReferenceDate()
displayLink?.invalidate()
displayLink1?.invalidate()
//MOVING BUTTON 1
moving1outlet.frame = CGRectMake(120, 400, 100, 100)
displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:")
displayLink?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
//DELAY TO MOVE BUTTON 2
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerAction", userInfo: nil, repeats: false)
}
func timerAction() {
//MOVING BUTTON 2
self.moving2outlet.frame = CGRectMake(120, 400, 100, 100)
self.displayLink1 = CADisplayLink(target: self, selector: "handleDisplayLink1:")
self.displayLink1?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
}
func updateTime() {
//COUNTING TIMER
let currentTime = NSDate.timeIntervalSinceReferenceDate()
//Find the difference between current time and start time.
var elapsedTime: NSTimeInterval = currentTime - startTime
//calculate the minutes in elapsed time.
let minutes = UInt8(elapsedTime / 60.0)
elapsedTime -= (NSTimeInterval(minutes) * 60)
//calculate the seconds in elapsed time.
let seconds = UInt8(elapsedTime)
elapsedTime -= NSTimeInterval(seconds)
//find out the fraction of milliseconds to be displayed.
let fraction = UInt8(elapsedTime * 100)
//add the leading zero for minutes, seconds and millseconds and store them as string constants
let strMinutes = String(format: "%02d", minutes)
let strSeconds = String(format: "%02d", seconds)
let strFraction = String(format: "%02d", fraction)
//concatenate minuets, seconds and milliseconds as assign it to the UILabel
timerlabel.text = "\(strMinutes):\(strSeconds).\(strFraction)"
}
func handleDisplayLink(displayLink: CADisplayLink) {
//POSITIONING BUTTON 1
var buttonFrame = moving1outlet.frame
buttonFrame.origin.y += -2
moving1outlet.frame = buttonFrame
if moving1outlet.frame.origin.y <= 50 {
displayLink.invalidate()
displayLink1?.invalidate()
}
}
func handleDisplayLink1(displayLink1: CADisplayLink) {
//POSITIONING BUTTON 2
var button4Frame = moving2outlet.frame
button4Frame.origin.y += -2
moving2outlet.frame = button4Frame
if moving2outlet.frame.origin.y <= 50 {
displayLink1.invalidate()
}
}
Thank you. Anton