ios动画和Android动画,在iOS中动画文本内容 – 等同于Android ValueAnimator

因为我没有找到定制的解决方案,我创建了我自己的:一个简单的动画师类,处理轻松:

// MyValueAnimation.h

typedef void (^MyAnimationBlock)(double animationValue);

@interface MyValueAnimation : NSObject

- (void)startAnimation:(MyAnimationBlock)animationBlock runtime:(NSUInteger)runtime delay:(NSUInteger)delay;

@end

// MyValueAnimation.m

#import "MyValueAnimation.h"

// Number of seconds between each animation step

#define kStepSize 0.05

@interface MyValueAnimation () {

NSTimer *timer;

NSUInteger totalRunTime; // Total duration of the animation (delay not included)

NSUInteger currentRuntime; // Time the animation is already running

MyAnimationBlock animationBlock;

}

@end

@implementation MyValueAnimation

- (void)startAnimation:(MyAnimationBlock)block runtime:(NSUInteger)runtime delay:(NSUInteger)delay {

if (timer != nil)

[timer invalidate];

timer = nil;

totalRunTime = runtime;

animationBlock = block;

currentRuntime = 0;

if (block != nil) {

if (delay > 0) {

// Wait to delay the start. Convert delay from millis to seconds

double delaySeconds = (double)delay / 1000.0;

timer = [NSTimer scheduledTimerWithTimeInterval:delaySeconds target:self selector:@selector(delayTick:) userInfo:nil repeats:false];

} else {

// Run the animation

timer = [NSTimer scheduledTimerWithTimeInterval:kStepSize target:self selector:@selector(animationTick:) userInfo:nil repeats:true];

}

}

}

- (void)delayTick:(NSTimer *)delayTimer {

// End of delay -> run animation

[delayTimer invalidate];

timer = [NSTimer scheduledTimerWithTimeInterval:kStepSize target:self selector:@selector(animationTick:) userInfo:nil repeats:true];

}

- (void)animationTick:(NSTimer *)animationTimer {

NSUInteger step = 1000 * kStepSize; // step size/length in milli seconds

currentRuntime += step;

double progress = MIN((double)currentRuntime / (double)totalRunTime,1.0);

// Progress is a value between 0 and 1. The easing function maps this

// to the animationValue which is than used inside the animationBlock

// to calculate the current value of the animiation

double animationValue = [self customEaSEOut:progress];

if (animationBlock != nil)

animationBlock(animationValue);

if (progress >= 1.0) {

// Animation complete

[timer invalidate];

timer = nil;

}

}

- (double)customEaSEOut:(double)t {

// Use any easing function you like to animate your values...

// http://rechneronline.de/function-graphs/

// http://sol.gfxile.net/interpolation/

return (1 - pow(1-t,2));

}

@end

// =============================================================

// Some code using the animation

- (void)animateValueFrom:(double)fromValue to:(double)toValue {

if (valueAnimation == nil)

valueAnimation = [[MyValueAnimation alloc] init];

MyAnimationBlock animationBlock = ^(double animationValue) {

double currentValue = fromValue + ((toValue - fromValue) * animationValue);

someLabel.text = [NSString stringWithFormat:"%dV",currentValue];

};

[valueAnimation startAnimation:animationBlock runtime:1500 delay:500];

}

也许不是最漂亮的解决方案,但它的作品:-)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值