我想动画一个UILabel淡入文字从左到右。下面的代码将整个UILabel立即消失,但我希望逐个字母或逐字逐句地去。此外,单词可以添加到这个文本,当这种情况发生时,我只想淡入新词,而不是整个UILabel再次。
__weak ViewController *weakSelf = self;
weakSelf.label.alpha = 0;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseIn
animations:^{ weakSelf.label.alpha = 1;}
completion:nil];
发布于 2015-05-19 22:50:34
我知道我会怎么做。我会制作一个包含渐变的面罩,并在标签的正面将面具动画化。
下面是我这样做的结果(不知道这是否正是您的想法,但也许这是一个开始):
发布于 2015-05-19 23:12:32
我曾经这样做过一次,取得了不错的效果。你可能不得不根据自己的喜好来调整它。
第一步是创建一个渐变图像,以匹配您希望文本淡入的方式。如果你的文字颜色是黑色,你可以试试这个。
- (UIImage *)labelTextGradientImage
{
CAGradientLayer *l = [CAGradientLayer layer];
l.frame = self.label.bounds;
l.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:0] CGColor], (id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor], nil];
l.endPoint = CGPointMake(0.0, 0.0f);
l.startPoint = CGPointMake(1.0f, 0.0f);
UIGraphicsBeginImageContext(self.label.frame.size);
[l renderInContext:UIGraphicsGetCurrentContext()];
UIImage *textGradientImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return textGradientImage;
}
该代码创建了一个应该从左到右淡出的黑色图像。
接下来,当您创建标签时,您需要根据我们刚刚生成的图像生成的模式来设置它的文本颜色。
self.label.alpha = 0;
self.label.textColor = [UIColor colorWithPatternImage:[self labelTextGradientImage]];
假设这是可行的,最后一步是将标签动画到视图中,并在发生这种情况时更改其文本颜色。
[UIView transitionWithView:self.label duration:0.2 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
self.label.textColor = [UIColor blackColor];
self.label.alpha = 1;
} completion:^(BOOL finished) {
}];
动画代码有点古怪,因为UILabel的textColor属性似乎不适合使用UIViewAnimation。
发布于 2018-09-27 14:49:36
我设法做到了这一点,并在UIView扩展中创建了一个函数:
@discardableResult public func addSlidingGradientLayer(withDuration duration: Double = 3.0, animationDelegate delegate: CAAnimationDelegate? = nil) -> CAGradientLayer {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.colors = [UIColor.white.cgColor, UIColor.clear.cgColor, UIColor.clear.cgColor]
gradientLayer.startPoint = CGPoint(x: -1.0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
let gradientAnimation = CABasicAnimation(keyPath: "startPoint")
gradientAnimation.fromValue = gradientLayer.startPoint
gradientAnimation.toValue = gradientLayer.endPoint
gradientAnimation.duration = duration
gradientAnimation.delegate = delegate
gradientLayer.add(gradientAnimation, forKey: "startPoint")
layer.mask = gradientLayer
return gradientLayer
}
首先,我们创建一个CAGradientLayer
实例,作为视图的掩码(在本例中是UILabel
)。
然后,我们创建一个CABasicAnimation
实例,用于动画startPoint
的CAGradientLayer
。这会使startPoint
从左向右滑动,并提供一些duration
值。
最后,我们也是CABasicAnimation
的代表。然后,如果我们坚持CAAnimationDelegate
协议,我们可以在动画完成后执行一些代码。
您可以这样使用这个扩展:
class ViewController: UIViewController {
@IBOutlet weak var someLabel: UILabel!
private func configureSomeLabel() {
someLabel.text = "Sliding into existence!"
someLabel.sizeToFit()
someLabel.addSlidingGradientLayer(withDuration: 3.0, animationDelegate: self)
}
override func viewDidLoad() {
super.viewDidLoad()
configureSomeLabel()
}
}
someLabel
已经连接到你的故事板上了。当我们将这个视图控制器指定为动画委托时,我们需要添加:
extension ViewController: CAAnimationDelegate {
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
someLabel.layer.mask = nil
}
}
我选择了移除掩蔽层。
希望这能帮到别人!
https://stackoverflow.com/questions/30337215
复制相似问题