开发者

CALayer - animating layer size blocks animation from running

开发者 https://www.devze.com 2023-01-31 14:18 出处:网络
I have a method in which a bunch of layers are positioned, and a single \"activated\" layer (b开发者_开发知识库asically a layer that the user has clicked on) is both positioned and resized at the same

I have a method in which a bunch of layers are positioned, and a single "activated" layer (b开发者_开发知识库asically a layer that the user has clicked on) is both positioned and resized at the same time. All the layers, including the activated layer are sublayers of a larger layer. Here is my method:

    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:0.7]
                     forKey:kCATransactionAnimationDuration];
    }];
    for (CALayer *layer in self.inactiveLayers) {
       ... do some positioning ...
    }
    CGRect newFrame = activeLayer.frame;
    newFrame.origin.x = 50.0;
    newFrame.origin.y = 50.0;
    newFrame.size.width = 100.0;
    newFrame.size.height = 100.0;
    activeLayer.frame = newFrame;
    [CATransaction commit];

The problem I'm experiencing is really weird. Using the code above, none of the animations run (not even the animations for the inactive layers). But as soon as I comment out the lines that set the size and width of the new frame, the animations magically start working again.

Is there any reason this should be happening?


Figured out the problem, I had a layout manager set on the superlayer of the layers I was trying to animate, and the layout code (which reset the sizes of the layers) was being called when the layer bounds changed. Disabling the layout manager for the duration of the animation solved the issue.


It is because the frame property of a CAlayer is not animatable. Use the bounds property instead and the position animation will run without a hitch.

0

精彩评论

暂无评论...
验证码 换一张
取 消