Challenge: Draw a Timer | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4659-drawing-in-ios-with-core-animation-and-core-graphics/lessons/7
1 Like

How can I make the line not move smoothly but jump from second to second?
Thank you very much in advance for your help.

Instead of the animation in layoutSubviews(), you could try something like this:

  1. Set up a property in ClockView:
var pointerTimer: Timer?
  1. In setup():
pointerTimer = Timer.scheduledTimer(withTimeInterval: 1,
                                    repeats: true) { timer in
  let seconds = Calendar.current.component(.second, from: Date())
  let secondsAngle = CGFloat(seconds) / 60.0 * 2.0 * .pi
  self.pointer.transform = CATransform3DMakeRotation(CGFloat(secondsAngle), 0, 0, 1)
}
  1. Add a method in ClockView to make sure the timer stops on close of view:
  override func removeFromSuperview() {
    pointerTimer?.invalidate()
    pointerTimer = nil
  }

Thank you very much for your help and understanding :grinning:

1 Like