簡單的UILabel動畫。

上週沒有些啥心得,或者是太懶了,不管如何?挖下了坑就是要埋。

公司一直對於動畫效果是沒啥做的,說真的對於這塊也是沒啥暸解,但下一火坑對於這塊好像有所要求,所以加減用一下。

希望的效果如下:

UILabel 建立。

NSString *textSring = [NSString stringWithFormat:@”資料更新中,請稍候!”];
CGRect rect = [textSring boundingRectWithSize:CGSizeMake(MAXFLOAT, 15.0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:14]} context:nil] ;
CGFloat textWidth = CGRectGetWidth(rect);
NSMutableArray *labelArray = [NSMutableArray arrayWithCapacity:textSring.length];
CGFloat totalWidth = self.loadingView.width*0.5-textWidth*0.5;
for (int i = 0; i < textSring.length; i ++) {
NSRange range = NSMakeRange(i, 1);
//获取字符宽度
NSString *contentString = [textSring substringWithRange:range]; //目标字符串
NSLog(@”contentString = %@”, contentString);
CGRect rect = [contentString boundingRectWithSize:CGSizeMake(MAXFLOAT, 15.0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:14]} context:nil] ;
CGFloat charWidth = CGRectGetWidth(rect);
UILabel *label = [UILabel new];
label.font = [UIFont systemFontOfSize:14.0];
label.text = contentString;
[label sizeToFit];
label.left = totalWidth;
label.centerY = self.loadingView.height*0.5;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.layer.opacity = 0.0f;
totalWidth += charWidth;
[labelArray addObject:label];
[self.loadingView addSubview:label];
CAAnimationGroup *group = [self labelAppearAnimationForLabel:label delay:i * 0.16];
[group setValue:[NSString stringWithFormat:@”%d”,i] forKey:@”name”];
[label.layer addAnimation:group forKey:@”labelAppear”];
}

而這段“ [self labelAppearAnimationForLabel:label delay:i * 0.16]”為加入動畫效果,源碼如下:

- (CAAnimationGroup *)labelAppearAnimationForLabel:(UILabel *)label delay:(CGFloat)delay{
//1. 透明度渐变
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@”opacity”];
opacityAnimation.fromValue = [NSNumber numberWithFloat:0.0];
opacityAnimation.toValue = [NSNumber numberWithFloat:1.0];
opacityAnimation.duration = 0.25;
opacityAnimation.beginTime = 0.0f + delay;
opacityAnimation.removedOnCompletion = NO;
opacityAnimation.fillMode = kCAFillModeForwards;
opacityAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
//2. 向下移动
CABasicAnimation *moveDownAnimation = [CABasicAnimation animationWithKeyPath:@”position”];
moveDownAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(label.layer.position.x, 61)];
moveDownAnimation.toValue = [NSValue valueWithCGPoint:label.layer.position];
moveDownAnimation.fillMode = kCAFillModeForwards;
moveDownAnimation.duration = 0.15f;
moveDownAnimation.beginTime = 0.0 + delay;
moveDownAnimation.removedOnCompletion = NO;
moveDownAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[opacityAnimation, moveDownAnimation];
group.duration = 1.6 + 0.05;
group.repeatCount = MAXFLOAT;
group.removedOnCompletion = NO;
group.autoreverses = NO;
group.fillMode = kCAFillModeForwards;
return group;
}
Show your support

Clapping shows how much you appreciated WU Hsien You’s story.