【发布时间】:2017-04-14 08:22:01
【问题描述】:
我有这段代码应该给 mainView 一个弹簧效果。
但我没有得到想要的效果,我只是通过 Storyboards 将 Transition 样式应用于 ViewController。
override func viewDidLoad()
{
super.viewDidLoad()
self.statusBarHidden = UIApplication.shared.isStatusBarHidden
UIApplication.shared.setStatusBarHidden(true, with: .none)
self.mainView = UIView(frame: CGRect(x: self.view.getWidth/20, y:self.view.getHeight/6, width:self.view.getWidth/1.3 , height: self.view.getHeight/1.3))
self.mainView.backgroundColor = UIColor.blue
self.mainView.center.x = self.view.center.x
self.mainView.center.y = self.view.center.y
self.mainView.transform = CGAffineTransform(scaleX: 0.8, y: 1.2) // initial view distorted scale so it has a starting point for the animation
self.view.addSubview(self.mainView)
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
self.mainView.transform = .identity // get back to original scale in an animated way
}, completion: nil)
}
能否请您帮忙将弹簧效果添加到 mainView 中?
谢谢
【问题讨论】:
-
在
viewDidAppear而不是viewDidLoad中开始动画。在viewDidLoad中,视图尚不可见且无法动画化。 -
只需在
UIView.animate块内调用view.layoutIfNeeded() -
@Sulthan 不错!谢谢!
标签: ios iphone swift animation view