UIView的图层经过动画处理后显示不正确 [英] UIView displays improperly after its layer has been animated

查看:43
本文介绍了UIView的图层经过动画处理后显示不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于UIView和Core Animation,我有一个我不明白的问题。这是我要执行的操作:

I have a problem I don't understand regarding UIViews and Core Animation. Here's what I want to do:


  • 通过将其作为子视图之一,在大视图上方显示了一个小视图。

  • 当我单击该视图内的按钮时,该视图应最小化并移至指定的CGRect。

  • 然后将视图从其超级视图中删除。

我第一次出现时,最小化并移动并删除视图,一切正常。但是,当我再次显示视图时,它显示在修改后的位置(即使应该通过调用 theView.frame = CGRectMake(600.0,160.0,339.0,327.0)将其设置为原始位置。 ; ),而视图中包含的所有不同响应者元素(按钮,文本视图等)的行为就像它们位于原始位置一样。就像视图和动画层使图层不同步一样,我不知道如何使它们重新同步。

The first time I present, minimize-and-move and remove the view, everything works fine. But when I present the view again, it displays at the modified position (even though it's supposed to be set at the original position by a call to theView.frame = CGRectMake(600.0, 160.0, 339.0, 327.0);), while all the different responder elements (buttons, textviews, etc.) contained in the view act as if they were at the original position. It's like the view and the layer gets dissynchronized by the animation, and I do not know how to get them back in sync.

具有类似 self.view.layer.frame = CGRectMake(600.0,160.0,339.0,327.0); 没有得到正确的结果。

Having something like self.view.layer.frame = CGRectMake(600.0, 160.0, 339.0, 327.0); does not get anything right.

我的最小移动动画代码如下:

My minimize-and-move animation code is given below:

    [CATransaction flush];
    CABasicAnimation *scale, *translateX, *translateY; 
    CAAnimationGroup *group = [CAAnimationGroup animation]; 
    group.delegate = delegate; 
    group.duration = duration; 

    scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
    translateX = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; 
    translateY = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"]; 

    scale.toValue = [NSNumber numberWithFloat:0.13];
    translateX.toValue = [NSNumber numberWithFloat:137.0];
    translateY.toValue = [NSNumber numberWithFloat:-290.0];

    group.animations = [NSArray arrayWithObjects: scale, translateX, translateY, nil];
    group.fillMode = kCAFillModeForwards; 
    group.removedOnCompletion = NO;

    [theView.layer addAnimation:group forKey:@"MyAnimation"];

如何在动画之后使图层回到视图?

How to get the layer back to the view after the animation?

推荐答案

如果删除这两行会发生什么?

What happens if you remove these two lines?

group.fillMode = kCAFillModeForwards; 
group.removedOnCompletion = NO;

这些行告诉核心动画的是您希望它继续显示处于动画的前进(最终)状态。同时,您实际上并没有在图层上设置变换以拥有用于动画的属性。

What you are telling core animation with those lines is that you want it to continue to display in the forward (final) state of the animation. Meanwhile, you didn't actually set the transform on the layer to have the properties you used for the animation. This would make things appear to be out of sync.

现在,您要遇到的问题是,删除这些行会导致您的转换恢复为动画完成时的开始状态。实际上,您需要做的是在图层上设置变换,以使它们在动画完成时保持其位置。

Now, the issue you're going to run into is that removing those lines will cause your transforms to revert back to the starting state when the animation has completed. What you need to do is actually set the transforms on the layer in order for them to hold their position when the animation completes.

另一种选择是保留这两行,然后实际上将动画从图层中明确删除,而不是像准备好恢复时提到的那样设置图层框架恢复到原始状态。您可以这样操作:

Another option is to leave the two lines in and then actually explicitly remove the animation from the layer instead of setting the layer frame as you mentioned when you are ready to revert back to the original state. You do this with:

[theView.layer removeAnimationForKey:@"MyAnimation"];

-removedOnCompletion属性告诉图层在完成动画后不要删除动画。现在,您可以显式删除它,它应该还原。

The -removedOnCompletion property told the layer not to remove the animation when it finished. Now you can explicitly remove it and it should revert back.

HTH。

这篇关于UIView的图层经过动画处理后显示不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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