Ios 设置多个UIScrollView计数器的动画
我从包含数字图像的多个UIScrollView构建了某种计数器。 计数器会定期更新新旧编号和动画持续时间。 Update实际上是for循环始终递增+1的计数器。 当我将其放入动画块时,动画的执行就像我刚刚将其更新为toValue一样,就像并没有for循环一样 我想要的是以线性方式执行动画,我想要看到每个数字(以防动画不是太快,而不是太快)。在一些奇怪的场景中,计数器只是跳到[fromValue,toValue]间隔中的某个随机数,没有动画,然后动画跳到toValue 有什么建议吗?这里有一些代码Ios 设置多个UIScrollView计数器的动画,ios,animation,uiscrollview,Ios,Animation,Uiscrollview,我从包含数字图像的多个UIScrollView构建了某种计数器。 计数器会定期更新新旧编号和动画持续时间。 Update实际上是for循环始终递增+1的计数器。 当我将其放入动画块时,动画的执行就像我刚刚将其更新为toValue一样,就像并没有for循环一样 我想要的是以线性方式执行动画,我想要看到每个数字(以防动画不是太快,而不是太快)。在一些奇怪的场景中,计数器只是跳到[fromValue,toValue]间隔中的某个随机数,没有动画,然后动画跳到toValue 有什么建议吗?这里有一些代码
- (void) setDigit:(UIScrollView*)digit withValue:(int)value
{
CGFloat height = digit.frame.size.height;
[digit setContentOffset:CGPointMake(0, height * value) animated:NO];
}
- (void) updateValue:(int) value
{
for (int i = 5; i >= 0; i--)
{
int a = value % 10;
value = value / 10;
[self setDigit:[digits objectAtIndex:i] withValue:a];
}
}
- (void) transitFromValue:(int)fromValue toValue:(int)toValue withDuration:(float)duration
{
[UIView animateWithDuration:duration
delay:0.0
options:UIViewAnimationCurveLinear
animations:^ {
for (int tValue = fromValue; tValue <= toValue; tValue++)
{
[self updateValue:tValue];
}
}
completion:^(BOOL finished) {
}];
}
-(void)setDigit:(UIScrollView*)带值的数字:(int)值
{
CGFloat height=数字.frame.size.height;
[数字setContentOffset:CGPointMake(0,高度*值)动画:否];
}
-(void)updateValue:(int)值
{
对于(int i=5;i>=0;i--)
{
int a=值%10;
数值=数值/10;
[self-setDigit:[digits objectAtIndex:i]带值:a];
}
}
-(void)transitFromValue:(int)fromValue到value:(int)to value with duration:(float)duration
{
[UIView animateWithDuration:持续时间
延迟:0.0
选项:UIViewAnimationCurveLinear
动画:^{
for(int tValue=fromValue;tValue我认为问题在于,当你启动for循环时,动画不会启动。它会通过bolck,然后看到最后一个值并将动画设置为该值。但我不能100%确定这是否是一个问题。
如果您尝试这样做:
int tValue=fromValue;
[UIView animateWithDuration:duration
delay:0.0
options:UIViewAnimationCurveLinear
animations:^ {
[self updateValue:tValue];
}
completion:^(BOOL finished) {
tValue++;
}];
[UIView animateWithDuration:duration
delay:0.0
options:UIViewAnimationCurveLinear
animations:^ {
[self updateValue:tValue];
}
completion:^(BOOL finished) {
tValue++;
}];
或者将该动画放入for循环我认为问题在于,当启动for循环时,动画不会启动。它会通过bolck,然后看到最后一个值并将其设置为该值。但我不能100%确定这是否是一个问题。
如果您尝试这样做:
int tValue=fromValue;
[UIView animateWithDuration:duration
delay:0.0
options:UIViewAnimationCurveLinear
animations:^ {
[self updateValue:tValue];
}
completion:^(BOOL finished) {
tValue++;
}];
[UIView animateWithDuration:duration
delay:0.0
options:UIViewAnimationCurveLinear
animations:^ {
[self updateValue:tValue];
}
completion:^(BOOL finished) {
tValue++;
}];
或者将动画放入for循环在动画块中设置的属性的最终值是系统将用于动画的值。您可以通过for循环设置数千个不同的值,但只有最后一个值将用于动画
最好的解决方案是一种递归方法,它可以设置一个步骤的动画,并在完成后再次调用
- (void) transitFromValue:(int)fromValue toValue:(int)toValue withDuration:(float)duration
{
[UIView animateWithDuration:duration
delay:0.0
options:UIViewAnimationCurveLinear
animations:^ {
[self updateValue:fromValue + 1];
}
completion:^(BOOL finished) {
if (finished && fromValue + 1 < toValue) {
[self transitFromValue:fromValue + 1 toValue:toValue withDuration:duration];
}
}];
}
-(void)transitFromValue:(int)fromValue to value:(int)to value with duration:(float)duration
{
[UIView animateWithDuration:持续时间
延迟:0.0
选项:UIViewAnimationCurveLinear
动画:^{
[自更新值:fromValue+1];
}
完成:^(布尔完成){
如果(完成(&fromValue+1
在动画块中设置的属性的最终值是系统将用于动画的值。您可以通过for循环设置数千个不同的值,但只有最后一个值将用于动画
最好的解决方案是一种递归方法,它可以设置一个步骤的动画,并在完成后再次调用
- (void) transitFromValue:(int)fromValue toValue:(int)toValue withDuration:(float)duration
{
[UIView animateWithDuration:duration
delay:0.0
options:UIViewAnimationCurveLinear
animations:^ {
[self updateValue:fromValue + 1];
}
completion:^(BOOL finished) {
if (finished && fromValue + 1 < toValue) {
[self transitFromValue:fromValue + 1 toValue:toValue withDuration:duration];
}
}];
}
-(void)transitFromValue:(int)fromValue to value:(int)to value with duration:(float)duration
{
[UIView animateWithDuration:持续时间
延迟:0.0
选项:UIViewAnimationCurveLinear
动画:^{
[自更新值:fromValue+1];
}
完成:^(布尔完成){
如果(完成(&fromValue+1