使用约束来动画全屏UIImageView

时间:2015-06-21 07:34:32

标签: ios animation constraints nslayoutconstraint

在我的iPhone(iOS)应用程序中,我使用全屏图像来显示"帮助"关于屏幕中可用的不同选项。我将前导,顶部,尾部和底部约束常量设置为" 0"指向超级视图。这种情况在iPhone 4S,5 / 5C / 5S,6和6+上运行良好,没有任何问题。
现在点击图像视图,我可以动画从右到左的图像。点击"帮助"在主视图上的按钮,帮助图像将从左到右动画回来。我在下面的代码中使用了动画:

self.helpViewLeadingConstraint.constant = 600;
[self.view layoutIfNeeded];


问题我面临的问题是,当动画正在进行时,我会看到一些警告。

    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:0x7fdda85a4b70 UIView:0x7fddaa8f05a0.trailingMargin == UIView:0x7fdda85a3460.trailing - 16>",
    "<NSLayoutConstraint:0x7fdda85a4c10 UIView:0x7fdda85a3460.leading == UIView:0x7fddaa8f05a0.leadingMargin + 600>",
    "<NSLayoutConstraint:0x7fddabb09e30 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fddaa8f05a0(375)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fdda85a4b70 UIView:0x7fddaa8f05a0.trailingMargin == UIView:0x7fdda85a3460.trailing - 16>

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.


我无法找到避免警告信息的解决方案。我们如何设置两个满足一次的约束而不产生任何不成功的消息?

2 个答案:

答案 0 :(得分:0)

您是否尝试在设置常量之前调用setNeedsUpdateConstraints? 这也可能有用https://github.com/SnapKit/SnapKit

答案 1 :(得分:0)

警告消息正确... 您必须同时更新前导约束和尾随约束。 Leading + 600和Trailing-600将保留图像尺寸。而不破坏现有的约束条件。

就我而言,我有两张全屏图像,其中一幅同时上升。一幅下降。

以下是同一代码:

func animateOverlayImage() {
    let viewHeight = self.view.frame.height
    UIView.animate(withDuration: Double(2.0), animations: {
        self.topImgTopConstraint.constant = -1 * viewHeight
        self.topImgBottomConstraint.constant = viewHeight

        self.bottomImgTopConstraint.constant = viewHeight
        self.bottomImgBottomConstraint.constant = -1 * viewHeight

        self.view.layoutIfNeeded()
    })
}