NSLayoutConstraint不断更新,但不像预期的那样动画

时间:2014-12-05 17:45:26

标签: ios animation nslayoutconstraint

只是预警 - 我很确定这个问题与动画中遗漏self.view.layoutIfNeeded()没有任何关系。

我期待约束'animatingSideConstraint'从viewWillAppear:中设置的值动画到viewDidAppear:中的新值集(和动画),这样我的搜索栏就会增长到导航栏的范围。

属性已设置,但未设置为动画。

这是我的代码:

@IBOutlet var animatingSideConstraint: NSLayoutConstraint!

override func viewDidLoad()
{
    super.viewDidLoad()

    //  Search Bar
    //
    searchBar.delegate = self

    if let searchBarContainerBounds = navigationController?.navigationBar.bounds
    {
        searchBarContainerView.bounds = searchBarContainerBounds
    }
}

override func viewWillAppear(animated: Bool)
{
    super.viewWillAppear(animated)

    //  Search bar
    //
    //      Set initial properties
    //
    animatingSideConstraint.constant = CGRectGetWidth(searchBarContainerView.bounds) * 0.5

    //      Initialise use
    //
    searchBar.becomeFirstResponder()
}

override func viewDidAppear(animated: Bool)
{
    super.viewDidAppear(animated)

    //  Search bar
    //
    UIView.animateWithDuration(1.0, animations: { () -> Void in
        self.animatingSideConstraint.constant = 0

        self.view.layoutIfNeeded()
    })
}

我已经将行self.animatingSideConstraint.constant = 0移动到动画块之前,但不出所料,它什么也没做。

感谢任何帮助!

更新:

override func viewDidAppear(animated: Bool)
{
    super.viewDidAppear(animated)

    //  Search bar
    //
    self.animatingSideConstraint.constant = 400

    NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "logData", userInfo: nil, repeats: true)

    UIView.animateWithDuration(1.0, animations: { () -> Void in
        self.view.layoutIfNeeded()
    })
}

func logData()
{
    NSLog("Constant: %@", self.animatingSideConstraint.constant)
}

给出输出:

2014-12-05 18:03:54.249 MyApp[6483:2353740] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x174083cf0 H:[UISearchBar:0x12dd092c0]-(NSSpace(0))-|   (Names: '|':UIView:0x170182080 )>",
    "<NSLayoutConstraint:0x174083d40 H:|-(400)-[UISearchBar:0x12dd092c0]   (Names: '|':UIView:0x170182080 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x1700856e0 h=--& v=--& H:[UIView:0x170182080(304)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x174083cf0 H:[UISearchBar:0x12dd092c0]-(NSSpace(0))-|   (Names: '|':UIView:0x170182080 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-12-05 18:03:54.361 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:54.455 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:54.556 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:54.655 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:54.755 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:54.856 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:54.955 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:55.056 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:55.155 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:55.255 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:55.355 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:55.455 MyApp[6483:2353740] Constant: 400
[repeated]

更新2

这是视觉格式的约束,我正在制作动画的约束是主要约束。

启动属性:

|-(half the search bar’s width)-[the search bar]-0-|

结束属性:

|-0-[the search bar]-0|

动画中途的中间属性,假设为简单起见,搜索栏的宽度为100

|-50-[the search bar]-0-|

更新3:

注意,前导约束是IBOutlet中引用的约束

enter image description here

0 个答案:

没有答案