【问题标题】:Animating UIImageView height constraint white space动画 UIImageView 高度约束空白
【发布时间】:2018-07-13 12:48:17
【问题描述】:

我正在尝试实现某种“视差”效果。

假设我在屏幕顶部有一张图片,高度为:180,下面有一个滚动视图。

初始状态为:

state initial

当我向上滚动超过 32 像素时,图像应该“停靠”起来,例如将他的高度设置为 45 像素,并进行动画处理。

最终状态应该是:

state final

在scrollViewDidEndDragging 中,我想将图像停靠在动画上。我使用了以下代码:

self.imageConstraint?.constant = 45.0

UIView.animate(withDuration: 1.0,
               delay: 0,
               options: [.beginFromCurrentState,
                         .allowAnimatedContent,
                         .allowUserInteraction],
               animations: {
                self.superview?.layoutIfNeeded()
}, completion: { _ in
    completion()
})

问题是图像设置为 45 高度,但只有图像,并且保留了一个空白空间(初始高度-最终高度)并且该空间是动画的。 基本上,没有减速,当 scrollDidEndDragging 我想为图像的高度设置动画时,这不能按预期工作。

看起来像这样:

during animation

我做错了什么?

【问题讨论】:

    标签: ios swift animation imageview constraints


    【解决方案1】:

    以这种方式编写你的代码,它会很有希望地工作,

    UIView.animate(withDuration: 0.5)
    {
        self. self.imageConstraint.constant = 45.0
        self.view.layoutIfNeeded();
    }
    

    如果没有,请让我。谢谢

    【讨论】:

    • 同样的事情,没有变化
    • 请检查您的约束。您是否使用了正确的约束?
    • 如果是这种情况,那么您应该使用 ScrollViewDelegate 并在滚动时更改图像约束。这是一个很棒的技巧
    • No .. Main View -> { 1.Safe Area, 2.Scroll View, 3.ImageView } @Najam 我试过这个,但我需要在拖拽结束时停靠
    • 朋友,你得先把view的顺序改一下,改成1-Safe Area, 2-ImageView, 3- ScrollView 只是调试用的,做完再告诉我
    猜你喜欢
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多