Reputation: 229
I'm trying to create an on-screen animation for iOS with Swift 4 on the Xcode IDE by drawing an object to the screen, waiting, adjusting the object's location, and redrawing. However, when I do this inside of a for loop, the screen simply waits and then draws the object in the final position. Why is it doing this? How can I animate the way that I want to? Is there an alternative way to animate that I should be considering?
My code is here:
var counter = 0
for n in 0...10{
counter = counter + 1
draw_wheel(counter)
sleep(1)
}
I simply see the object I am trying to draw in its final orientation after 10 seconds.
Upvotes: 2
Views: 277
Reputation: 770
If you want to create step by step animation over the time, you should consider to use CADisplayLink instead of Timers or loops.
Basically CADisplayLink is a timer object that allows your application to synchronize its drawing to the refresh rate of the display and draw according to timestamp.
func createDisplayLink() {
let displaylink = CADisplayLink(target: self,
selector: #selector(step))
displaylink.add(to: .current,
forMode: .default)
}
@objc func step(displaylink: CADisplayLink) {
print(displaylink.timestamp)
}
Upvotes: 1