iOS 给 Storyboard 创建的 view 添加动画效果

使用 storyboard, xib 创建的视图, 视图的约束是用的 Autolayout , 所以实现动画效果也要通过改变约束的值,而不是直接改变 frame

 

示例: 在控制器上添加一个 blueView, blueView 上添加一个 greenView, 现改变 blueView 的宽度实现动画效果

 

一. 改变约束值实现动画

将 blueView 的宽度拉成属性 BlueView_Width, 改变 BlueView_Width 的 constant 值, 然后在UIView.animate 中添加                 strongSelf.view.layoutIfNeeded(), 更新controller View 的子视图布局, 不添加的 strongSelf.view.layoutIfNeeded() 没有动画效果

blueView 是 View 的子视图, blueView 的宽度改变时, view 的 viewWillLayoutSubviews 会执行

        @IBOutlet weak var blueView: UIView!
        @IBOutlet weak var greenView: UIView!
        @IBOutlet weak var BlueView_Width: NSLayoutConstraint!

        let blueWidth = BlueView_Width.constant + 10
        BlueView_Width.constant = blueWidth
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.8, options: .curveEaseIn, animations: { [weak self] in
            if let strongSelf = self {
                strongSelf.view.layoutIfNeeded()
            }
            
        }, completion: nil)

二.使用 frame 实现动画

 view 的 viewWillLayoutSubviews  不会执行

错误:

blueView 的宽度改变了, 但是 greenView 的宽度没有随着改变

UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.8, options: .curveEaseIn, animations: { [weak self] in
            if let strongSelf = self {
                let blueWidth = strongSelf.blueView.frame.width + 10
                strongSelf.blueView.frame.size.width = blueWidth
                strongSelf.view.layoutIfNeeded()

            }
        }, completion: nil)

因为 blueView 和 greenView  的约束是通过 Autolayout 创建的, 所以改变 blueView frame 时, 它的子视图并不会随之改变, view 的 viewWillLayoutSubviews 不会执行, 要让 greenView 宽度改变, 需要改变 greenView 的 frame

UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.8, options: .curveEaseIn, animations: { [weak self] in
            if let strongSelf = self {
                let blueWidth = strongSelf.blueView.frame.width + 10
                strongSelf.blueView.frame.size.width = blueWidth
                strongSelf.greenView.frame.size.width = 70
                strongSelf.view.layoutIfNeeded()
            }
            
        }, completion: nil)

如果使用 blueView 的 layoutIfNeeded() 方法, 需要写在改变 greenView 宽度之前, 写在之后没有效果

UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.8, options: .curveEaseIn, animations: { [weak self] in
            if let strongSelf = self {
                let blueWidth = strongSelf.blueView.frame.width + 10
                strongSelf.blueView.frame.size.width = blueWidth
                strongSelf.blueView.layoutSubviews()
                strongSelf.greenView.frame.size.width = 70
            }
            
        }, completion: nil)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值