如何修复动画视图时出现的旧框架故障?

时间:2019-05-03 19:11:08

标签: ios swift animation uiview uiviewanimation

我有一个包含分页器视图(FSPagerView)的标题视图,其中有一个项目(FSPagerViewCell)包含条形图,2019年,年审文本。我正在尝试在某些时候按以下代码缩小Header:

private func setSelfHeightAnimated(height: CGFloat)
{
    UIView.animate(withDuration: 0.2, animations: {
        self.selfHeightAnchor.constant = height
        self.superview?.layoutIfNeeded()
    })
}

private func setPagerHeightAnimated(height: CGFloat)
{
    UIView.animate(withDuration: 0.2, animations: {
        self.pagerHeightAnchor.constant = height
        self.layoutIfNeeded()
//            self.pagerView.layoutIfNeeded()
//            self.pagerView.layoutSubviews()
//            self.pagerView.cellForItem(at: 0)?.layoutIfNeeded()
//            self.pagerView.cellForItem(at: 0)?.setNeedsDisplay()
//            self.superview?.layoutIfNeeded()
//            self.setNeedsDisplay()
//            self.pagerView.setNeedsDisplay()
    })
}

但是,旧的帧故障非常明显。可能缺少什么?这是视频:https://streamable.com/owsdi

enter image description here

2 个答案:

答案 0 :(得分:0)

您有第一个布局和第二个布局,以及一个从第一个布局到第二个布局的动画。
当前,在动画过程中第一个布局淡出,第二个布局淡入,这给您带来了“毛刺”。
我认为您要实现的是:
-顶部(历史记录,2019年,年度回顾)应保持其高度,并且
-仅下面的列应收缩/扩展,而下面的表则可以上下滑动。
一种解决方案可能是仅对包含两列的子视图的高度设置动画。
如果将标题视图设置为采用其高度作为内容,那么我希望所显示的效果不会出现故障。

编辑:

如果您最终必须对整个上部进行动画处理,则一种可能性(我认为是丑陋的)可能是在动画开始之前立即将“ History”,“ 2019”和“ Year”的alpha值设置为0。然后这些文字只会消失,从而避免了在不同位置出现双重图像的故障。

答案 1 :(得分:0)

尝试这样避免毛刺。

private func setSelfHeightAnimated(height: CGFloat)
{
    self.selfHeightAnchor.constant = height

    UIView.animate(withDuration: 0.2, animations: {
        self.layoutIfNeeded()
        self.viewDidLayoutSubviews()
    })
}

private func setPagerHeightAnimated(height: CGFloat)
{
    self.pagerHeightAnchor.constant = height

    UIView.animate(withDuration: 0.2, animations: {
        self.layoutIfNeeded()
        self.viewDidLayoutSubviews()
    })
}