• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 滑动后调整 super View 大小时,UIPageViewController View 会跳转

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:43:22 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个 View Controller ,它以编程方式将 UIPageViewController 添加到 subview (我们称之为“覆盖”)。由于页面 View Controller 的每个页面都有不同的高度,我在“overlay” subview 上添加了一个 NSLayoutConstraint。

在每次滑动时,我都会计算即将到来的 View Controller 的高度并相应地调整覆盖 View 的大小:

- (void)resizeOverlayToBottomOfUIView *)element {

   // resize overlay in parent view controller so it shows the element
   float bottomOfElement = element.frame.origin.y + element.frame.size.height + 20;
   [self.parentViewController.overlayHeightConstraint setConstant:bottomOfElement];

   [self.parentViewController.overlayView setNeedsUpdateConstraints];
   [self.parentViewController.overlayView setNeedsLayout];
   [self.parentViewController.overlayView layoutIfNeeded];
}

一切都按预期工作...直到我想用动画更改叠加层大小。
我用动画 block 包裹了上述方法的最后两行:

[UIView animateWithDuration:0.25f animations:^{
   [self.parentViewController.overlayView setNeedsLayout];
   [self.parentViewController.overlayView layoutIfNeeded];
}];

现在滑动完成后,覆盖 View 的高度会随着动画的变化而变化。

问题在于,当动画播放时,页面 View Controller 的内容会快速回到屏幕外的某个位置并滑回。我尝试添加约束以确保页面 View Controller 内容的固定宽度,但似乎没有任何作用诀窍。

任何关于如何在不影响页面 View Controller View 的情况下为父 View 设置动画的提示都将受到高度赞赏!



Best Answer-推荐答案


我最近遇到了同样的问题。这显然有效:

- (void)pageViewControllerUIPageViewController *)pageViewController didFinishAnimatingBOOL)finished previousViewControllersNSArray *)previousViewControllers transitionCompletedBOOL)completed
{
    if (completed)
    {
        CGFloat height = [self calculateHeight];
        [CATransaction begin];
        [CATransaction setCompletionBlock:^{
            self.heightConstraint.constant = height;
            [UIView animateWithDuration: 0.25
                                  delay: 0
                                options: UIViewAnimationOptionCurveEaseInOut
                             animations:^{

                                 [self.view layoutIfNeeded];
                             }
                             completion:^(BOOL finished) {

                             }];
        }];
        [CATransaction commit];
    }
}

我的理解是一个动画会干扰另一个动画。
CATransaction 拦截当前动画并在当前动画完成后执行它的完成 block 。
这对我来说很好。

关于ios - 滑动后调整 super View 大小时,UIPageViewController View 会跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23491061/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap