动画UIView框架,子视图UIScrollView并不总是动画 [英] Animating a UIView frame, subview UIScrollView doesn't always animate

查看:97
本文介绍了动画UIView框架,子视图UIScrollView并不总是动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个示例中。

当我为tabBarController设置动画(用于全屏效果)时,我正在为PhotoViewerViewController的框架设置动画。 PhotoViewer使用uiscrollview生成Apple的照片应用程序所具有的相同效果。无论出于何种原因,它有时会与我的PhotoViewer框架一起动画,有时则不会。

I'm animating the PhotoViewerViewController's frame when I animate out the tabBarController (for fullscreen effect). The PhotoViewer uses a uiscrollview to generate the same sort of effect Apple's photos app does. For whatever reason sometimes it animates along with my PhotoViewer frame, and sometimes it doesn't.

您可以在第一个示例中看到它在增加帧大小时跳转但是减小帧大小(并恢复标签栏)时动画很好。

You can see in the first example it jumps when increasing the frame size, but animates nicely when decreasing the frame size (and restoring the tab bar).

但是在这个示例当照片是垂直的时,它会向两个方向跳跃。

However in this example when the photo is vertical it jumps in both directions.

在这两种情况下,如果我完全放大照片使用滚动视图,它可以在两个方向上正确动画。

In both cases if I zoom in on the photo at all with the scrollview, it animates correctly in both directions.

我知道证据存在,但我无法理解这里发生的事情。

I know the evidence is there, but I can't put my finger on what's happening here.

这是我的动画块:

void (^updateProperties) (void) = ^ {
    self.navigationController.navigationBar.alpha = hidden ? 0.0f : 1.0f;
    self.navigationController.toolbar.alpha = hidden ? 0.0f : 1.0f;
    if (self.tabBarController) {

        int height = self.tabBarController.tabBar.bounds.size.height;
        for(UIView *view in self.tabBarController.view.subviews)
        {
            int newHeight;
            UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
            if (UIInterfaceOrientationIsPortrait(orientation)) {
                newHeight = hidden ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.height - height;
            } else {
                newHeight = hidden ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.width - height;
            }


            if([view isKindOfClass:[UITabBar class]])
            {
                [view setFrame:CGRectMake(view.frame.origin.x, newHeight, view.frame.size.width, view.frame.size.height)];
            } 
            else 
            {
                CGRect newFrame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, newHeight);
                [view setFrame:newFrame];

                // update our VC frame with animation
                [self.view setFrame:newFrame];

            }

        }
    }

};

从SO上的帖子改编的代码: UITabBar不会隐藏

Code adapted from a post on SO: UITabBar wont hide

github

推荐答案

一般情况下,如果是动画有时工作,有时不工作,这是因为在后一种情况下,延迟执行会通过将相关财产设置为其最终值来有效取消它们。

In general, if animations sometimes work and sometimes don't, it's because in the latter case a delayed perform is causing them to be effectively cancelled by setting the property concerned to its final value.

In UIScrollView 的特殊情况通常会在动画块关闭后调用 -layoutSubviews 而没有动画一个运行循环。出于这个原因,我通常会尝试避免在该方法中进行任何布局,在视图层次结构中我有 UIScrollView (因为特别是在iOS 5之前, UIScrollView 在设置自己需要布局时真的很高兴。

In the particular case of UIScrollView it often happens that -layoutSubviews is called without animation one run loop after your animation blocks have closed. For this reason I usually try to avoid doing any layout in that method where I have a UIScrollView in the view hierarchy (because particularly prior to iOS 5, UIScrollView is really trigger-happy on setting itself to need layout).

我建议删除你对的电话 - [EGOPhotoImageView layoutScrollViewAnimated:] 来自 -layoutSubviews 并将其添加到被覆盖的 -setFrame:相反。

I would recommend removing your call to -[EGOPhotoImageView layoutScrollViewAnimated:] from -layoutSubviews and add it to an overridden -setFrame: instead.

如果您可以定位iOS4及以上版本,请查看 UIViewAnimationOptionLayoutSubviews 。使用基于块的动画作为选项可能只是工作而不改变其他任何东西。

If you can target iOS4 and above, take a look at UIViewAnimationOptionLayoutSubviews too. Using a block-based animation with this as an option might just work without changing anything else.

这篇关于动画UIView框架,子视图UIScrollView并不总是动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆