获取由UIView的animateWithDuration创建的动画的引用,或通过任何其他方式对其进行标识 [英] Get a reference of an animation created by UIView's animateWithDuration or identify it by any other means

查看:62
本文介绍了获取由UIView的animateWithDuration创建的动画的引用,或通过任何其他方式对其进行标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我像这样制作动画:

  [UIView animateWithDuration:0.5 
delay:0.0
选项:UIViewAnimationOptionCurveEaseInOut
动画:^ {
someView.frame = CGRect(0,100,200,200);
}
完成:无];

然后假设我在 someView 中有一些子视图,它们的 layoutSubviews 方法已被覆盖。我想在 layoutSubviews 中识别出一个正在进行的动画,并根据我检测到的动画是我创建的还是例如由设备旋转引起的动画来做出相应的动作。



要获得正在进行的动画,我使用以下代码:

  for(NSString * [animationKey in [self.layer animationKeys]))
{
CAAnimation * animation = [self.layer animationForKey:animationKey];
// ...做某事...
}

现在我将使用 CABasicAnimation 我可以使用以下命令设置任意属性:

  [myAnimation setValue:@ mySuperCoolAnimation  forKey:@ AnimationName]; 

然后在另一侧检查该键。但由于我的视图层次结构复杂,我想避免使用 CABasicAnimation



因此,为简单起见,有一种方法可以做到这一点使用 UIView animateWithDuration:

解决方案

您能否举一个标志说动画正在进行中,或将其降低到完成块中?



还是像使用CABasicAnimation一样使用当前的CATransaction存储任意键?一个鲜为人知的事实是,总是有一个CATransaction,它会在运行循环的结尾(在您自己的代码完成之后)自动提交并运行动画,然后在完成时有选择地触发完成处理程序-正如我在书中所解释的,事实上,动画是这样工作的:



http://www.apeth.com/iOSBook/ch17.html#_animation_transactions



(请参见

由于各种原因,这很酷:




  • 即使此时您的代码与动画无关,也可以从您的代码中使用当前的CATransaction。


  • 您可以进行自己的交易,包装动画并提交(这可能是您所追求的解决方案;我不太清楚)


  • CATransaction有一个完成处理程序。我经常使用此技巧来执行某些动画之后必须执行的操作。


  • CATransaction在动画结束后触发其完成处理程序,即使它们完成不是您的动画;我将此技巧用作延迟性能的一种特殊形式,可以等到运行时自己内部生成的动画之一结束。


  • CATransaction可以存储任意键/值对,就像CALayer和CAAnimation一样。我经常使用它作为通过事务传递信号的一种方式,在完成处理程序运行时被拾取。




很抱歉,在这里没有提出任何具体的解决方案,但我只是建议您了解CATransaction可能会使您找到适合自己情况的方案。


Let's say that I make an animation like so:

[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                               someView.frame = CGRect(0, 100, 200, 200);
                             }
                 completion:nil];

And Let's say that I have subviews inside someView that have their layoutSubviews method overridden. I would like to identify an ongoing animation inside that layoutSubviews and make corresponding actions depending if the animation I detected was created by me, or if it is for example an animation caused by device rotation.

To get the animations in progress I use the following code:

for (NSString *animationKey in [self.layer animationKeys])
{
    CAAnimation *animation = [self.layer animationForKey:animationKey];
    // ... Do something ...
}

Now If I would be using CABasicAnimation I could set arbitrary properties using:

[myAnimation setValue:@"mySuperCoolAnimation" forKey:@"AnimationName"];

And then check that key on the other side. But I would like to avoid using CABasicAnimation because of my complex view hierarchy.

So for simplicity sake is there a way to do something like that using UIView animateWithDuration:

解决方案

Could you just raise a flag to say your animation is in progress, and lower it in the completion block?

Or use the current CATransaction to store an arbitrary key, as you would have done with the CABasicAnimation? It is a little-known fact that there is always a CATransaction, which automatically commits and runs the animations at the end of the run loop (after your own code finishes), and then optionally fires a completion handler when it finishes - that, in fact, is how animation works, as I explain in my book:

http://www.apeth.com/iOSBook/ch17.html#_animation_transactions

(See esp. "The Truth About Transactions", further down that page.)

This is cool for various reasons:

  • The current CATransaction is available from your code, even when your code has nothing to do with animation at that moment.

  • You can make your own transaction, wrapping your animations, and commit it (this might be the solution you're after; I can't quite tell)

  • CATransaction has a completion handler. I often use this trick to do something that must follow on right after one of my animations.

  • CATransaction fires its completion handler after its animations finish, even if they are not your animations; I use this trick as a specialized form of delayed performance, to wait until one of the runtime's own internally generated animations ends.

  • CATransaction can store an arbitrary key/value pair, just like CALayer and CAAnimation. I often use this as a way of passing a signal through the transaction, to be picked up when the completion handler runs.

Sorry not to propose any specific solution here, but I'm just suggesting that knowing about CATransaction might allow you to work out something appropriate to your case.

这篇关于获取由UIView的animateWithDuration创建的动画的引用,或通过任何其他方式对其进行标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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