查看动画不起作用-iOS Swift 3 [英] View Animation does not work - ios Swift 3

查看:117
本文介绍了查看动画不起作用-iOS Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码应该给mainView一个弹跳的效果。

I have this piece of code that supposed to give a spring effect to mainView.

但是我没有得到想要的效果,我只能通过Storyboard将Transition样式应用于ViewController。

But I don't get the desired effect, I only get the Transition style applied to the ViewController via Storyboards.

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吗?

Can you please help in getting the spring effect to mainView?

谢谢

推荐答案

要使用动画,您应该了解视图控制器的生命周期。如果您要描述动画,这很关键,因为您应该了解何时以及为什么开始动画。

To work with animation you should understand view controller life circle. This is crucial in case that you describe becouse you should understand when and why to start animation.

首先请阅读有关官方< a href = https://developer.apple.com/reference/uikit/uiviewcontroller rel = nofollow noreferrer>文档。

代码,但请尝试了解为什么它现在可以正常工作:

Code, but try to understand why it now is working :

  class ViewController: UIViewController {

    var mainView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        addMainView()
    }

    override func viewDidAppear(_ animated: Bool) {
       super.viewDidAppear(animated)
       animateMainView()
    }

    func addMainView() {
        self.mainView = UIView(frame: CGRect(x: self.view.frame.width/20, y:self.view.frame.height/6, width:self.view.frame.width/1.3 , height: self.view.frame.height/1.3))

        self.mainView.backgroundColor = UIColor.blue
        self.mainView.center.x = self.view.center.x
        self.mainView.center.y = self.view.center.y

        self.view.addSubview(self.mainView)
    }

    func animateMainView() {

        self.mainView.transform = CGAffineTransform(scaleX: 0.8, y: 1.2) // initial view distorted scale so it has a starting point for the animation

        UIView.animate(withDuration: 0.9, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
            self.mainView.transform = .identity // get back to original scale in an animated way
        }, completion: nil)
    }
}

动画示例:

这篇关于查看动画不起作用-iOS Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆