使用CABasicAnimationanimation框架属性

我试图做一个精确的“翻译”这个UIView基于块的animation代码:

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

改用CABasicAnimation

我完全知道frame属性实际上是底层的positionboundsanchorPoint的组合,如下所示: http : //developer.apple.com/library/mac/#qa/qa1620/_index html的
…我已经做了这样的解决scheme,使用两个CABasicAnimations一个设置位置 ,一个边界 ,它适用于这一个视图。

但问题是我在我的视图里面有子视图。 someView有一个UIScrollViewtypes的子视图,其中我还放置了另一个types为UIImageView的子视图。 UIScrollView子视图的autoresizingMask设置为UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight 。 如果我使用UIView基于块的版本,这一切都完美的作品,但是当我尝试使用CABasicAnimations子视图开始行为意外(即得到resize不正确的宽度)。 所以看来autoresizingMask在使用CABasicAnimations时无法正常工作。 我注意到子视图也没有收到对setFrame的调用,尽pipe父视图的frame属性在对图层的位置和边界进行更改之后确实发生了变化。

这就是为什么我想知道什么是正确的代码复制与CABasicAnimation ,当使用UIView的animateWithDuration方法发生了什么。

我完全知道框架属性实际上是底层的位置,边界和锚点的组合

好,但重要的是要意识到frame不是图层的animation属性。 如果要使用CABasicAnimation进行animation制作,则必须使用可用于图层animation的属性。 CALayer文档将每个这样的属性都标记为明确的“animation”。 使用boundsposition的想法是正确的。

因此,这个代码实质上是你以前做的事情:

 [CATransaction setDisableActions:YES]; // set final bounds and position v.layer.bounds = CGRectMake(0,0,200,200); v.layer.position = CGPointMake(100,200); // cause those changes to be animated CABasicAnimation* a1 = [CABasicAnimation animationWithKeyPath:@"bounds"]; a1.duration = 0.5; CABasicAnimation* a2 = [CABasicAnimation animationWithKeyPath:@"position"]; a2.duration = 0.5; [v.layer addAnimation:a1 forKey:nil]; [v.layer addAnimation:a2 forKey:nil]; 

但是,该代码对v.layer的任何子层的大小没有影响。 ( v的子视图是由v.layer的子图层绘制的)。不幸的是,你正在试图解决这个问题。 我相信,通过降低到层次和直接显式核心animation,你放弃了自动调整,这发生在视图的水平。 因此,您还需要为子图层设置animation。 这是视图animation为你做的。

这是iOS的一个不幸的function。 Mac OS X具有图层约束(CAConstraint),在图层级别执行自动调整在视图级别(和更多)执行的操作。 但是iOS缺less这个function。