首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UISlider平滑更改值

UISlider平滑更改值
EN

Stack Overflow用户
提问于 2013-09-15 21:26:48
回答 3查看 13.6K关注 0票数 7

我有一个为AVAdioRecording设置位置的UIslider:

代码语言:javascript
复制
CGRect frame = CGRectMake(50.0, 230.0, 200.0, 10.0);
                     aSlider = [[UISlider alloc] initWithFrame:frame];
                     // Set a timer which keep getting the current music time and update the UISlider in 1 sec interval
                     sliderTimer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
                     // Set the maximum value of the UISlider
                     aSlider.maximumValue = player.duration;
                     // Set the valueChanged target
                     [aSlider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
                     [self.ViewA addSubview:aSlider];




 - (void)updateSlider {
// Update the slider about the music time

[UIView beginAnimations:@"returnSliderToInitialValue" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:1.3];

aSlider.value = player.currentTime;

[UIView commitAnimations];
}

- (IBAction)sliderChanged:(UISlider *)sender {
// Fast skip the music when user scroll the UISlider
[player stop];
[player setCurrentTime:aSlider.value];
[player prepareToPlay];
[player play];
}

我想问三个问题。

1)为什么值更改的动画不起作用? 2)为什么滑块位置只在我从按钮上松开手指时才移动,并且没有跟随它移动? 3)使用NSTimer是最好的方法吗?我听说NSTimer很消耗内存……

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-09-15 21:33:39

为什么value动画不起作用

显然,您找到了value属性。查看文档,你会看到这句话

若要渲染从当前值到新值的动画过渡,应改用setValue:animated:方法。

所以,正如文档所说的那样,使用

代码语言:javascript
复制
[aSlider setValue:player.currentTime animated:YES];

为什么只有在松开手指时才会收到事件

只有在释放手指时才会出现事件的原因是,滑块不是连续的。来自continuous属性的文档:

如果为YES,则滑块将更新事件连续发送到关联目标的操作方法。如果为NO,则滑块仅在用户释放滑块的thumb控件以设置最终值时发送操作事件。

NSTimer不是最好的方法

不,使用NSTimer来模拟这样的变化肯定不是最好的方法,我要说的是使用计时器是非常糟糕的做法。它不仅是无效的和可能不精确的,而且你还失去了对动画的内置支持。

如果你真的不能在没有计时器的情况下做到这一点,那么你至少应该用CADisplayLink代替NSTimer。它是用来处理UI更新的(与NSTimer相反,后者不是)。

票数 16
EN

Stack Overflow用户

发布于 2013-09-15 21:33:04

您可能应该使用这些:

  1. 在创建滑块时将滑块属性continuous设置为YES

在您的示例中,使用aSlider.continuous = YES;

  • Use setValue:animated方法,

在您的例子中是[aSlider setValue:player.currentTime animated:YES];

票数 5
EN

Stack Overflow用户

发布于 2015-06-11 18:31:41

我正在寻找一个解决方案,我添加一个目标到我的UISlider,这将只触发一次,当用户停止移动滑块。

我想保存一次选定的值,而不是每次更新时都保存,这就是为什么我取消选择continous with NO的原因。我刚刚意识到,将continous设置为NO将不再使滑块具有动画效果。所以经过一些尝试,我发现,如果你像这样结合使用self.slider setValue:animated:[UIView animateWithDuration:animations:]UISlider将会有动画效果:

添加目标

代码语言:javascript
复制
[self.sliderSkill addTarget:self 
                     action:@selector(skillChange) 
           forControlEvents:UIControlEventValueChanged];

目标方法

代码语言:javascript
复制
- (void)skillChange{

    CGFloat fValue = self.sliderSkill.value;

    [UIView animateWithDuration:0.5f animations:^{
        if( fValue < 1.5f ){
            [self.slider setValue:1 animated:YES];
        } else if( fValue > 1.5f && fValue < 2.5f ){
            [self.slider setValue:2 animated:YES];
        } else {
            [self.slider setValue:3 animated:YES];
        }
    }];
}

也许有人可以用这个!

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18812907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档