UIView 动画不一致的结果 [英] UIView Animation Inconsistent Result

查看:31
本文介绍了UIView 动画不一致的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UIView 动画在屏幕上滑动我的视图.我的视图控制器中有一个 UIScrollView,其中有我的 UIViews.

I am trying to make use of UIView animation to slide my views across the screen. I have a UIScrollView in my view controller, in which I have my UIViews.

我也有这个方法:

-(void)translateView:(UIView *)view toRect:(CGRect)rect withDuration:(CGFloat)duration
{
    [UIView animateWithDuration:duration
    animations:^
    {
        view.frame = rect;
    }
    completion:^(BOOL finished)
    {
        //Finished
    }];
}

我称之为在一段时间内以动画方式将我的 UIView 移动到我选择的 CGRect.我有一个创建并滑出 7 个视图的循环.这很好用,我像下面这样称呼它,循环当然在不同的视图上调用了 7 次:

I call this to move my UIView in an animated fashion to a CGRect of my choice over a certain time. I have a loop which creates and slides out 7 views. This works great, I call it like below, the loop of course calling this 7 times on different views:

[self translateView:cell toRect:translationRect withDuration:0.7];

但是,之后我不能立即再次调用它,只是没有任何反应.虽然,假设我在 2 秒 NSTimer 后再次调用它,动画确实运行了,但是当我滚动 UIScrollView 时,我刚刚设置动画的视图会跳回到它之前的 CGRect.

However, I can't then call this again immediately afterwards, just nothing happens. Although, let's say I call this again after a 2 second NSTimer, the animation does run, but then when I scroll my UIScrollView, the view I just animated jumps back to its previous CGRect.

推荐答案

您的故事板已启用自动布局.当自动布局运行时,它会根据您在情节提要中设置(或自动设置)的约束来设置视图的框架.这意味着如果您更改视图的框架,自动布局将在下次运行时撤消您的更改.

Your storyboard has autolayout enabled. When autolayout runs, it sets the frames of your views based on the constraints you set up (or that were automatically set up for you) in the storyboard. This means that if you change the frame of a view, autolayout will undo your change the next time it runs.

系统会在不同时间自动触发自动布局,包括在您滚动滚动视图时.这就是为什么您在滚动时看到视图跳回"的原因.

The system triggers autolayout automatically at various times, including when you scroll a scroll view. That's why you're seeing your view "jump back" when you scroll.

如果您不需要自动布局,您可以将其关闭(请参阅这个答案 如果您需要帮助将其关闭).然后你可以在你的故事板中使用旧的弹簧和支柱"布局系统,它允许你根据需要设置视图的框架.

If you don't need autolayout, you can turn it off (see this answer if you need help turning it off). Then you can use the older "springs and struts" layout system in your storyboard, which lets you set the frames of your views however you want.

如果您确实需要自动布局,您需要学习如何使用自动布局制作动画.有两种基本方法.一种方法是修改约束,以便自动布局将视图放置在您想要的位置,然后在动画块中执行 [view layoutIfNeeded] 以将视图动画到其新位置.另一种方法是使用 NSTimerCADisplayLink 为约束本身设置动画.

If you do need autolayout, you need to learn how to make animations work with autolayout. There are two basic approaches. One approach is to modify the constraints so that autolayout will put the view where you want, and then do [view layoutIfNeeded] in an animation block to animate the view to its new position. The other approach is to animate the constraints themselves using an NSTimer or a CADisplayLink.

WWDC 2012 视频Session 228 - 掌握自动布局的最佳实践"解释了您的问题,并从 30 分 45 秒开始深入讨论了这两种技术.如果您需要在自动布局中使用动画,我强烈建议您观看 WWDC 2012 中的视频以及其他与自动布局相关的视频.

The WWDC 2012 video "Session 228 - Best Practices for Mastering Auto Layout" explains your problem, and discusses these two techniques in depth, starting at 30m45s. If you need to use animation with autolayout, I highly recommend you watch the video, and the other autolayout-related videos from WWDC 2012.

这篇关于UIView 动画不一致的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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