问题描述
在iOS Objective C中,我需要带有渐变的文本填充动画,该动画将从底部到顶部(垂直)填充渐变。我在iOS的核心图形或动画方面不擅长。 以前有没有人做过这样的任务。
任何帮助将不胜感激。
解决方法
这是个主意。使用两个标签并为第二个标签设置动画。这有其局限性,要想走得更远,您需要使用标签的图层或将其转换为UIImage的图层,然后对其进行动画处理。这是从中心向外动画的,这并不完全是您正在寻找,但目前只是一个主意。
情节提要仅包含一个按钮,您只需将其连接即可启动动画。
编辑
以前,它是从中心向外动画的,但是我将其更改为从底部到顶部进行动画处理。
- ( void ) viewDidLoad
{
[super viewDidLoad];
// Start label
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake ( 10,50,100,20 )];
// Configure
label.text = @"Label";
label.textColor = UIColor.redColor;
label.sizeToFit;
[self.view addSubview:label];
// The label we animate in
UILabel * label1 = [[UILabel alloc] initWithFrame:CGRectMake ( 10,20 )];
// Configure
label1.text = @"Label";
label1.textColor = UIColor.blueColor;
label1.clipsToBounds = YES;
label1.contentMode = UIViewContentModeBottom;
label1.sizeToFit;
// We keep a weak reference to it
[self.view addSubview:label1];
self.label1 = label1;
}
- ( IBAction ) buttonAction:( id ) sender
{
CGRect f = self.label1.frame;
CGRect b = self.label1.bounds;
// Need to animate both the bounds and the frame's position
self.label1.frame = CGRectMake( f.origin.x,f.origin.y + f.size.height / 2,f.size.width,f.size.height );
self.label1.layer.bounds = CGRectMake( 0,b.size.width,0 );
[UIView animateWithDuration:2
animations:^{
self.label1.frame = f;
self.label1.layer.bounds = b;
}];
}
请注意,您需要向视图控制器添加弱属性,例如
@property (nonatomic,weak) UILabel * label1;
因为以后需要使用它才能进行动画处理。