我正在实现 UISlider,它工作正常。
CGFloat y = self.view.frame.size.width - (self.view.frame.size.height-20) + 350;
self.currentTimeSlider = [[UISlider alloc] initWithFrame:CGRectMake(self.view.frame.size.width-285,y,self.view.frame.size.width-120,self.view.frame.size.height-20)];
self.currentTimeSlider.minimumValue=0;
self.currentTimeSlider.maximumValue = self.audioPlayer.getAudioDuration;
[self.currentTimeSlider addTarget:self
action:@selector(sliderChanged:)
forControlEvents:(UIControlEventTouchUpInside | UIControlEventTouchUpOutside)];
self.currentTimeSlider setBackgroundColor:[UIColor clearColor]];
self.currentTimeSlider.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
- (void)updateTime:(NSTimer *)timer {
NSLog(@"slider = %f",self.currentTimeSlider.value);
textView.hidden=NO;
NSString *bgFilePath = [[NSBundle mainBundle] pathForResource:@"inside"
ofType:@"png"];
bgimg = [[UIImage alloc] initWithContentsOfFile:bgFilePath];
[bgImageView setImage:bgimg];
if([self.audioPlayer getCurrentAudioTime]==0)
{
NSLog(@"song finished.");
NSString *bgFilePath = [[NSBundle mainBundle] pathForResource:@"screen5"
ofType:@"jpg"];
bgimg = [[UIImage alloc] initWithContentsOfFile:bgFilePath];
[bgImageView setImage:bgimg];
textView.hidden=YES;
}
long currentPlaybackTime = [self.audioPlayer getCurrentAudioTime];
if ((currentPlaybackTime / [self.audioPlayer getAudioDuration]) > self.currentTimeSlider.value )
{
self.currentTimeSlider.value = currentPlaybackTime / [self.audioPlayer getAudioDuration];
self.timeElapsed.text = [NSString stringWithFormat:@"%@",
[self.audioPlayer timeFormat:[self.audioPlayer getCurrentAudioTime]]];
self.duration.text = [NSString stringWithFormat:@"%@",
[self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration] - [self.audioPlayer getCurrentAudioTime]]];
}
else
{
self.timeElapsed.text = [NSString stringWithFormat:@"%@",
[self.audioPlayer timeFormat:[self.audioPlayer getCurrentAudioTime]]];
NSLog(@"time elapsed = %d",self.timeElapsed.text);
self.duration.text = [NSString stringWithFormat:@"%@",
[self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration] - [self.audioPlayer getCurrentAudioTime]]];
}
}
- (IBAction)sliderChanged:(id)currentTimeSlider
{
NSLog(@"sliderValue = %f",self.currentTimeSlider.value);
NSLog(@"sliderValuesss = %f",self.currentTimeSlider.value);
[self.audioPlayer setCurrentAudioTime:self.currentTimeSlider.value];
[NSTimer scheduledTimerWithTimeInterval:0.02
target:self
selector:@selector(updateTime:)
userInfo:nil
repeats:NO];
}
我的问题是 slider slider 不会自行拖动。但是在手动 slider 上拖动计时器更新以及歌曲。但是,如果我评论最小值和最大值,则 slider 会自行拖动,但手动滑动会从头开始播放歌曲。
self.currentTimeSlider.minimumValue=0;
self.currentTimeSlider.maximumValue = self.audioPlayer.getAudioDuration;
请问如何实现手动和自动 slider 拖动。
谢谢,
最佳答案
您正在使用 NSTimer
,它在单独的线程上运行,而不是在主线程上运行,并且 UI 由主线程处理。
因此,要解决您的问题,您需要在主线程上触发一个事件以使用此方法更新您的 UI: performSelectorOnMainThread:withObject:waitUntilDone:NO
在该选择器方法中编写您的代码以更新您的自定义 slider 值。
还有一个建议,不要每次都创建自定义 slider ,可以在viewDidLoad时创建,此时在updateTime
,会造成内存问题。而不是在此方法中仅将 slider 值更改为更新的值。
关于ios - slider 拇指不会在自己的 iOS 上拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24550822/