I am trying to have a image of a star "breathe" in a loop. Getting bigger and then smaller and then repeating itself. I want this to continue while my app is still running and being responsive. And when I go to another view in my app , then I want the animation to stop.
Here is what I got
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.animateStars()
}
}
func animateStars() {
UIView.animate(withDuration: 4, animations : {
self.star.transform = CGAffineTransform(scaleX: 2.5, y: 2.5)
}) { finished in
NSLog("Star is big. Finished = \(finished)")
}
UIView.animate(withDuration: 4, animations : {
self.star.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
}) { finished in
NSLog("Star is small. Finished = \(finished)")
}
}
This does not loop tough. And I dont know how to make it loop without it running like mad in the background, when the user click on a button with segue, to go to another view.
I have found one solution in Objective C, but I cant seem to understand how I should translate it to Swift 3.0. Example here: https://stackoverflow.com/a/31978173/5385322
[UIView animateWithDuration:1
delay:0
options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat
animations:^{
yourView.transform = CGAffineTransformMakeScale(1.5, 1.5);
}
completion:nil];
Thank you for your time and suggestions! //Simon