使用constraint时, animateWithDuration不起作用的解决方法

        调用UIView的animateWithDuration设计简单的动画是很方便的。但使用uiconstraint进行界面设计时,animateWithDuration的动画不起作用。问题可能出现在:

        1.没有调用layoutIfNeeded。

        2.调用layoutIfNeeded的对象不正确。


     apple文档对layoutIfNeeded的描述是:

Lays out the subviews immediately.


Use this method to force the layout of subviews before drawing. Using the view that receives the message as the root view, this method lays out the view subtree starting at the root.


    在  IOS 8 by tutorials 一书有以下描述:

   Whenever you manually change any layout constraints you must call layoutIfNeeded() on the superview of the views that are affected by constraint change.

为了使constraint的变化显示出来,调用layoutIfNeeded的view应该是受影响的view的superview

      stackoverflow上有关于这个问题的回答:

http://stackoverflow.com/questions/12622424/how-do-i-animate-constraint-changes



demo:

<span style="font-family:Microsoft YaHei;font-size:14px;">import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var redView: UIView!
    @IBOutlet weak var grayView: UIView!
    @IBOutlet weak var redWidthConstraint: NSLayoutConstraint!
    @IBOutlet weak var redHorizontalSpace: NSLayoutConstraint!
    @IBOutlet weak var grayWidthConstraint: NSLayoutConstraint!
    @IBOutlet weak var grayHorizontalSpace: NSLayoutConstraint!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func animate(sender: AnyObject) {
        
        UIView.animateWithDuration(2.0, animations: {
            
            self.redHorizontalSpace.constant = self.redHorizontalSpace.constant + 100
            self.grayHorizontalSpace.constant = self.grayHorizontalSpace.constant + 100
    
        })
    }
}</span>

outlet对应如下:




初始位置:



1.没有调用layoutIfNeeded,redView和grayView的位置瞬间变化,没有动画出现。


2.在self.redView上调用layoutIfNeeded,redView出现动画,grayVIew位置还是瞬移:


<span style="font-family:Microsoft YaHei;font-size:14px;">    @IBAction func animate(sender: AnyObject) {
        
        UIView.animateWithDuration(2.0, animations: {
            
            self.redHorizontalSpace.constant = self.redHorizontalSpace.constant + 100
            self.grayHorizontalSpace.constant = self.grayHorizontalSpace.constant + 100
            self.redView.layoutIfNeeded()
    
        })
    }</span>

动画过程的一个截图:



3.在self.view上调用layoutIfNeeded,两个view都出现动画:

<span style="font-family:Microsoft YaHei;font-size:14px;">    @IBAction func animate(sender: AnyObject) {
        
        UIView.animateWithDuration(2.0, animations: {
            
            self.redHorizontalSpace.constant = self.redHorizontalSpace.constant + 100
            self.grayHorizontalSpace.constant = self.grayHorizontalSpace.constant + 100
            self.view.layoutIfNeeded()
    
        })
    }</span>


4.在superview的superview上调用layoutIfNeeded同样可以出现动画:

import UIKit

class NextViewController: UIViewController {

    @IBOutlet weak var grayWidth: NSLayoutConstraint!
    @IBOutlet weak var redVerticalSpace: NSLayoutConstraint!
    @IBOutlet weak var grayView: UIView!
    @IBOutlet weak var redView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func animate(sender: AnyObject) {
        
        UIView.animateWithDuration(2.0, animations: {

        self.grayWidth.constant = self.grayWidth.constant + 100
        self.redVerticalSpace.constant = self.redVerticalSpace.constant + 100
        self.view.layoutIfNeeded()
            
        })
    }

}




动画过程中的截图:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值