UILabel animation beyond bounds of UILabel
calayer, core-animation, ios, uilabel
Solution
You are moving the `UILabel`'s layer itself with animation, and the `UILabel`'s `clipsToBounds` (UIView)/`masksToBounds` (CALayer) is limiting the `UILabel`'s layer itself as a whole.
To make this clearer: If you would put something inside the `UILabel`, or try to render something bigger on the label's layer, it will be clipped by the mask. But if you just moved the label to somewhere else on the parent view - then it would stay whole, as the mask moves with it...
Even when animating, this is the exact same situation. The mask moves with the layer.
So the solution is containing the `UILabel` within another `UIView`, which itself has the `clipsToBounds` set. This would also allow you to create a subclass of `UIView` which is a mini-controller, manages its own labels, animates them etc. on demand with simple interface on the `UIView` subclass. (I guess that this is what you're doing and you just forgot to set `clipsToBounds` on the container view...)
Problem
I have a label and I want a "speedometer" effect; when a new value is assigned to the label's text I'd like the old value to scroll up and the new to come in from below. I am agnostic as to how to achieve this. This is what I am using currently; ``` CATransition *animation = [CATransition animation]; animation.duration = .2f; animation.type = kCATransitionPush; animation.subtype = kCATransitionFromBottom; animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; animation.removedOnCompletion = YES; [mylabel.layer addAnimation:animation forKey:@"changeTextTransition"]; mylabel.text = @"new text"; ``` This works pretty dang well EXCEPT that the transition at top and bottom goes outside the bounds of the label before fading. I'd like the transition effect to be contained within the bounds of the label. I have tried to create a mask for the layer, and have the layer mask to bounds, but this had no effect: ``` UIImageView *maskView = [[UIImageView alloc] initWithImage:labelMask]; // this image is a png that is the same dimensions as the label, and is 100% opaque and colored white maskView.frame = mylabel.frame; mylabel.layer.mask = maskView.layer; mylabel.layer.masksToBounds = YES; ``` I even tried an alternate method of mask creation: ``` CALayer *maskLayer = [CALayer new]; maskLayer.frame = mylabel.frame; maskLayer.contents = (id)(labelMask.CGImage); // same image as above maskLayer.contentsRect = mylabel.bounds; mylabel.layer.mask = maskLayer; mylabel.layer.masksToBounds = YES; ``` This had no effect. What else may I try to prevent the layer animation from leaking past the bounds of the UILabel?