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

查看:143
本文介绍了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.

推荐答案

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

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.

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

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视频会议228 - 最佳实践掌握自动布局解释了你的问题,并从30m45开始深入讨论这两种技术。如果您需要使用带有自动布局的动画,我高度建议您观看视频以及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天全站免登陆