2

我正在使用 animateWithDuration 来更改 UISlider 的值:

[UIView animateWithDuration:1.0f
                      delay:0.0f
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{ [myUISlider setValue:10]; }
                 completion:^(BOOL finished){ }];

问题是,我需要能够在 UISlider更改时显示它的当前值。这甚至可以使用 animateWithDuration 吗?我尝试创建一个 UISlider 子类并覆盖 setValue 以希望在更改滑块时访问滑块的值:

-(void)setValue:(float)newValue
{
    [super setValue:newValue];

    NSLog(@"The new value is: %f",newValue);
}

但是该代码仅在动画块的最后被调用。以一种非常有意义的方式,因为我真的只调用了一次 setValue。但我希望动画块会以某种方式在其内部机制中一遍又一遍地调用它,但似乎并非如此。

如果 UIView animateWithDuration 在这里是一个死胡同,我想知道是否有更好的方法来用其他东西实现同样的功能?也许我不知道 SDK 的另一个光滑的小块驱动部分允许动画不仅仅是 UIView 参数?

4

3 回答 3

2

我认为最好的处理方法是编写一个自定义代码,Slide您可以像进度条一样创建它,在 github 上有很多演示。

你也可以用NSTimer它来做,但我不认为这是一个很好的方法。

当您点击 时,您可以创建一个timer

_timer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(setValueAnimation) userInfo:nil repeats:YES];

并设置一个 ivar:_value = oldValue;

setValueAnimation方法中:

- (void)setValueAnimation
{
    if (_value >= newValue) {
        [_timer invalidate];
        _value = newValue;
        [self setVale:_value];
        return;
    }

    _value += .05;

    [self setVale:_value];

}

更新

如何添加块:

第一个:您可以定义一个块处理程序:

typedef void (^CompleteHandler)();

第二:创建您的块并将其添加到 userInfo 中:

CompleteHandler block = ^(){
    NSLog(@"Complete");
};

NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:block,@"block", nil];

第三:制作NSTimer

_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(setValueAnimation:) userInfo:userInfo repeats:YES];

4th:实现你的计时器方法:

- (void)setValueAnimation:(NSTimer *)timer
{
    if (_value >= newValue) {
        [_timer invalidate];
        _value = newValue;
        [self setVale:_value];

        // also can use [_delegate complete];

        CompleteHandler block = [timer.userInfo objectForKey:@"block"];
        if (block) {

              block();

        }
        return;
    }

    _value += .05;

    [self setVale:_value];

}
于 2013-03-15T03:01:57.623 回答
0

我不知道是否有任何通知可以“利用”来获取 UIView 动画“步骤”,但是您可以使用 NSTimer“手动”执行动画,这将允许您不断更新您的值.

于 2013-03-15T02:54:45.540 回答
0

如果有使用 UIView 的开箱即用解决方案 - 我全神贯注。这个动画框架(只有 2 个类!) - PRTween https://github.com/chris838/PRTween将让您访问方便的计时功能以及更改的值。

这是我的一个更新项目https://github.com/jdp-global/KACircleProgressView/

PRTweenPeriod *period = [PRTweenPeriod periodWithStartValue:0 endValue:10.0 duration:1.0];

PRTweenOperation *operation = [PRTweenOperation new];
operation.period = period;
operation.target = self;
operation.timingFunction = &PRTweenTimingFunctionLinear;
operation.updateSelector = @selector(update:)

[[PRTween sharedInstance] addTweenOperation:operation]

- (void)update:(PRTweenPeriod*)period {
  [myUISlider setValue:period.tweenedValue];
}
于 2013-12-03T06:48:13.897 回答