我有一个标签,我想要一个“速度表”效果;当一个新的值被分配到标签的文本时,我希望旧的值向上滚动,而新的值从下面来。对于如何实现这一点,我是不可知论的。这是我目前正在使用的;
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";除了顶部和底部的转换在褪色之前超出了标签的范围之外,这是非常有效的。我想把过渡效果包含在标签的范围内。
我尝试为该层创建一个掩码,并将该层掩码设置为边界,但这没有产生任何效果:
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;我甚至尝试了另一种创建掩码的方法:
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;这没有效果。
我还能做些什么来阻止图层动画泄漏到UILabel的边界上呢?
发布于 2014-07-07 05:46:57
您正在用动画移动UILabel的图层本身,而UILabel的clipsToBounds (UIView)/masksToBounds (CALayer)正在限制UILabel的层本身作为一个整体。
为了更清楚地说明这一点:如果您要在UILabel中放置一些东西,或者尝试在标签层上渲染更大的东西,它将被掩码剪切。但是如果你把标签移到父视图的其他地方,它就会保持完整,因为面具会随之移动.
即使在动画中,这也是完全相同的情况。面罩随图层移动。
因此,解决方案是将UILabel包含在另一个UIView中,该UIView本身具有clipsToBounds集。这还允许您创建一个UIView的子类,它是一个迷你控制器,管理自己的标签,按需动画它们,并在UIView子类上使用简单的接口。(我想这就是您要做的,您只是忘记在容器视图上设置clipsToBounds .)
https://stackoverflow.com/questions/24603217
复制相似问题