繁体   English   中英

UILabel超越UILabel的动画

[英]UILabel animation beyond bounds of UILabel

我有一个标签,我想要一个“车速表”效果; 当为标签的文本分配一个新值时,我希望旧值向上滚动,新值从下面进入。 我不知道如何实现这一目标。 这就是我目前使用的;

  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的边界?

你正在用动画移动UILabel的图层,而UILabelclipsToBounds (UIView)/ masksToBounds (CALayer)正在限制UILabel的图层本身。

为了使这个更清楚:如果你把里面的东西UILabel ,或尝试呈现一些标签的层上更大,它会被屏蔽被裁剪。 但是,如果您只是将标签移动到父视图上的其他位置 - 那么它将保持整体,因为蒙版随之移动...

即使在制作动画时,这也是完全相同的情况。 掩模随着图层移动。

所以解决方案是在另一个UIView包含UILabel ,它本身设置了clipsToBounds 这也可以让你创建一个UIView的子类,它是一个迷你控制器,管理自己的标签,根据需要动画它们等,并在UIView子类上使用简单的接口。 (我想这就是你正在做的事情,你只是忘了在容器视图上设置clipsToBounds ...)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM