我有一个为AVAdioRecording设置位置的UIslider:
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很消耗内存……
发布于 2013-09-15 21:33:39
为什么value动画不起作用
显然,您找到了value属性。查看文档,你会看到这句话
若要渲染从当前值到新值的动画过渡,应改用
setValue:animated:方法。
所以,正如文档所说的那样,使用
[aSlider setValue:player.currentTime animated:YES];为什么只有在松开手指时才会收到事件
只有在释放手指时才会出现事件的原因是,滑块不是连续的。来自continuous属性的文档:
如果为
YES,则滑块将更新事件连续发送到关联目标的操作方法。如果为NO,则滑块仅在用户释放滑块的thumb控件以设置最终值时发送操作事件。
NSTimer不是最好的方法
不,使用NSTimer来模拟这样的变化肯定不是最好的方法,我要说的是使用计时器是非常糟糕的做法。它不仅是无效的和可能不精确的,而且你还失去了对动画的内置支持。
如果你真的不能在没有计时器的情况下做到这一点,那么你至少应该用CADisplayLink代替NSTimer。它是用来处理UI更新的(与NSTimer相反,后者不是)。
发布于 2013-09-15 21:33:04
您可能应该使用这些:
continuous设置为YES,在您的示例中,使用aSlider.continuous = YES;
setValue:animated方法,在您的例子中是[aSlider setValue:player.currentTime animated:YES];
发布于 2015-06-11 18:31:41
我正在寻找一个解决方案,我添加一个目标到我的UISlider,这将只触发一次,当用户停止移动滑块。
我想保存一次选定的值,而不是每次更新时都保存,这就是为什么我取消选择continous with NO的原因。我刚刚意识到,将continous设置为NO将不再使滑块具有动画效果。所以经过一些尝试,我发现,如果你像这样结合使用self.slider setValue:animated:和[UIView animateWithDuration:animations:],UISlider将会有动画效果:
添加目标
[self.sliderSkill addTarget:self
action:@selector(skillChange)
forControlEvents:UIControlEventValueChanged];目标方法
- (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];
}
}];
}也许有人可以用这个!
https://stackoverflow.com/questions/18812907
复制相似问题