(iOS 5.1,XCode 4.4)
编辑:目前(在 iOS 7.0 上),该图层似乎始终忽略第一个非动画更改,并且始终从原始值设置动画。我无法再重现对视图调整大小的依赖。
我有一个 CALayer,它的位置首先用 [CATransaction setDisableActions:YES] 更改(所以没有动画),然后直接用 [CATransaction setDisableActions:NO] (动画)更改。通常,这会导致动画从第一次更改中设置的位置到第二次更改中设置的位置。但是,我发现我的代码从初始位置动画到第二次更改的位置。
经过大量的测试和调试,我发现它依赖于包含在更改之前调整大小的图层的 UIView。代码重现(iphone单视图模板,添加QuartzCore.framework):
#import <QuartzCore/QuartzCore.h>
#import "TAViewController.h"
@interface TAViewController ()
@property (nonatomic, strong) UIView *viewA;
@property (nonatomic, strong) CALayer *layerA;
@end
@implementation TAViewController
- (IBAction)buttonPressed
{
self.viewA.frame = CGRectMake(0, 30, 320, 250);
[self setPosition:CGPointMake(0, 100) animated:NO];
[self setPosition:CGPointMake(0, 150) animated:YES];
}
- (void)setPosition:(CGPoint)position animated:(BOOL)animated
{
[CATransaction begin];
if(animated) {
[CATransaction setDisableActions:NO];
[CATransaction setAnimationDuration:5];
} else {
[CATransaction setDisableActions:YES];
}
self.layerA.position = position;
[CATransaction commit];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.viewA = [[UIView alloc] init];
self.viewA.backgroundColor = [UIColor darkGrayColor];
self.viewA.frame = CGRectMake(0, 30, 320, 300);
[self.view addSubview:self.viewA];
self.layerA = [CALayer layer];
self.layerA.backgroundColor = [UIColor redColor].CGColor;
self.layerA.anchorPoint = CGPointZero;
self.layerA.frame = CGRectMake(0, 0, 320, 100);
[self.viewA.layer addSublayer:self.layerA];
}
@end