我现在已经学习swift一周了,在创建了我的第一个使用Weather API的应用程序后,我想创建简单的动画:LaunchScreen.storyboard中有一个带背景的图像,我想将图像设置为动画收缩到0,以便我的其他ViewController看起来正常。我做了类似的东西,但是有问题,动画完成后出现主ViewController并且里面没有动画。此外,我希望这个图像稍微增加它的大小然后缩小 - 也许还有另一种方法可以在一个UIView.animate中做到这一点?
afterLaunchVC.swift:
class afterLaunchVC: UIViewController {
@IBOutlet weak var logoImg: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
//increasing the size
UIView.animate(withDuration: 0.1, animations: ({
self.logoImg.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
}), completion: nil)
//shrinking the image after increasing
UIView.animate(withDuration: 1.0, animations: ({
self.logoImg.transform = CGAffineTransform(scaleX: 0.00001, y: 0.00001)
}), completion:{ //performingSegue
finished in self.performSegue(withIdentifier: "MainVC", sender: self)
})
}
}
正如您所看到的,UIView.animate完成我设置了performSegue并关闭了它的动画,但无法在MainVC.swift中获取动画(IBOutlets正常显示但没有动画)
MainVC.swift:
class MainVC: UIViewController {
@IBOutlet weak var topLabel: UILabel!
@IBOutlet weak var hourLabel: UILabel!
@IBOutlet weak var remainingTimeLabel: UILabel!
var time : TimeTrack!
override func viewDidLoad() {
super.viewDidLoad()
topLabel.center.y = self.view.frame.height + 100
hourLabel.center.y = self.view.frame.height + 100
remainingTimeLabel.center.y = self.view.frame.height + 100
UIView.animate(withDuration: 2.6, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 3.0, options: [], animations: ({
self.topLabel.center.y = self.view.frame.height/2
}), completion: nil)
UIView.animate(withDuration: 2.5, delay: 0.1, usingSpringWithDamping: 1.0, initialSpringVelocity: 3.0, options: [], animations: ({
self.hourLabel.center.y = self.view.frame.height/2
}), completion: nil)
UIView.animate(withDuration: 2.4, delay: 0.2, usingSpringWithDamping: 1.0, initialSpringVelocity: 3.0, options: [], animations: ({
self.remainingTimeLabel.center.y = self.view.frame.height/2 - 30
}), completion: nil)
// further code...
}
我希望有人会解释这里的错误,因为我对这种语言不熟悉
答案 0 :(得分:2)
我建议您熟悉UIViewController lifecycle。
因此,最好将这些动画放在viewDidAppear
中。
当视图显示时,您希望这些内容设置为动画,视图加载时和显示时间之间存在差异。
查看确实加载:当视图加载到内存
时视图确实显示:在视图加载后调用(在viewWillAppear
后调用,在屏幕上显示视图时右键