slider thumb不会在自己的iOS上拖动

时间:2014-07-03 10:10:15

标签: ios objective-c uislider

我正在实施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];


}

我的问题是滑块拇指不会自行拖动。但是在手动滑块上拖动定时器更新以及歌曲。但是如果我评论最小值和最大值,那么滑块会自行拖动,但是手动幻灯片从头开始播放歌曲。

self.currentTimeSlider.minimumValue=0;
self.currentTimeSlider.maximumValue = self.audioPlayer.getAudioDuration;

请知道如何实现手动和自动滑块拖动。

谢谢,

1 个答案:

答案 0 :(得分:1)

您正在使用 NSTimer ,它运行在一个单独的线程上,而不是在主线程上,UI由主线程处理。

因此,要解决您的问题,您需要在主线程上触发事件以使用此方法更新UI: performSelectorOnMainThread:withObject:waitUntilDone:NO

在该选择器方法中编写代码以更新自定义滑块值。

还有一个建议,每次都不要创建自定义滑块,你可以在viewDidLoad时创建它,现在它在updateTime中,它会引起内存问题。而不是在此方法中仅将滑块值更改为更新的滑块值。

相关问题