我正在尝试使恒星“呼吸”在一个循环中。变大然后变小,然后重复。我希望在我的应用程序仍在运行并响应时继续执行此操作。当我在应用程序中转到另一个视图时,我希望动画停止播放。
这就是我得到的
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)")
}
}
Run Code Online (Sandbox Code Playgroud)
这并不困难。而且,我不知道如何在用户单击带有segue的按钮时不让它在后台疯狂运行的情况下使其循环。
我在Objective C中找到了一种解决方案,但是我似乎无法理解如何将其转换为Swift 3.0。此处的示例:https : //stackoverflow.com/a/31978173/5385322
[UIView animateWithDuration:1
delay:0
options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat
animations:^{
yourView.transform = CGAffineTransformMakeScale(1.5, 1.5);
}
completion:nil]; …Run Code Online (Sandbox Code Playgroud)