什么阻止子层与父移动?

时间:2012-05-02 21:09:48

标签: ios core-animation calayer

使用下面的代码,sublayer1&在需要的地方绘制子层2。当动画发生时,sublayer1按预期移动,而sublayer2最初向上移动然后向右移动。 (2不会在1内保持同一位置)

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Create a blue sublayer 
    sublayer1 = [CALayer layer];
    sublayer1.backgroundColor = [[UIColor blueColor] CGColor];
    sublayer1.frame = CGRectMake(30, 30, 120, 190);
    sublayer1.position = CGPointMake(90, 130);
    [self.view.layer addSublayer:sublayer1];

    // Create a sublayer within blue layer
    CALayer *sublayer2 = [CALayer layer];
    sublayer2.bounds = CGRectMake(0, 0, 60, 60);
    sublayer2.frame = CGRectMake(30, 50, 60, 60);
    sublayer2.contents = (id)[[UIImage imageNamed:@"sun.png"] CGImage];
    [sublayer1 addSublayer:sublayer2];

    [self performSelector:@selector(moveBlueLayer) withObject:nil afterDelay:1.0];
}

- (void)moveBlueLayer
{
    // Create a new position for animation to move to
    CGPoint endPostition = CGPointMake(190, 285);

    [CATransaction setAnimationDuration:4];
    sublayer1.bounds = CGRectMake(130, 190, 120, 190);
    sublayer1.position = endPostition;
}
他们应该一起搬家吗?有什么想法他们不干嘛?我试图坚持解决“隐含的”问题。动画现在。

2 个答案:

答案 0 :(得分:0)

不应[sublayer1 addSublayer:flowerlayer];[sublayer1 addSublayer:sublayer2];

答案 1 :(得分:0)

从底部向上的第3行引起了不稳定的行为。

- (void)moveBlueLayer
{
   // Create a new position for animation to move to
   CGPoint endPostition = CGPointMake(190, 285);

   [CATransaction setAnimationDuration:4];
   // Next line causing the problem
   // sublayer1.bounds = CGRectMake(130, 190, 120, 190);
   sublayer1.position = endPostition;
}