CollectionView头内的ProgressView

时间:2015-02-21 01:21:01

标签: ios objective-c

我正在尝试在标题内创建一个进度视图,该视图会与歌曲当前进度同步更新。我有进度条工作正常的逻辑。它只是我似乎无法获得标题来显示progressView。我需要获取标题的确切单元格并根据我的计时器设置进度。如下。但是下面的代码让我在访问特定的indexPath时遇到了崩溃。如何访问标头的indexPath?

- (void)updateTime
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    CurrentArtistHeader *cell = (CurrentArtistHeader *)[self.collectionView cellForItemAtIndexPath:indexPath];

    float duration = CMTimeGetSeconds(self.player.currentItem.duration);
    float current = CMTimeGetSeconds(self.player.currentTime);
    cell.progressView.progress = (current/duration);
}

-(void)playCurrentArtist:(NSDictionary *)currentArtist
{
    NSString *streamString = [currentArtist objectForKey:@"stream_url"];
    NSString *urlString = [NSString stringWithFormat:@"%@?client_id=%@", streamString,CLIENT_ID];
    NSURL *URLFromString = [NSURL URLWithString:urlString];
    self.player = [AVPlayer playerWithURL:URLFromString];
    [self.player play];
    self.playerIsPlaying = YES;
    [self.collectionView reloadData];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.player currentItem]];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
}

1 个答案:

答案 0 :(得分:0)

需要做的是在viewForSupplementaryElement中标记标题单元格,然后使用标记标题设置我的计时器的进度。

    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

        if ([kind isEqualToString:CSStickyHeaderParallaxHeader]) {
            CurrentArtistHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                   withReuseIdentifier:@"header"
                                                                          forIndexPath:indexPath];
            if (self.hostCurrentArtist[@"user"]) {
                header.tag = 50;
              } 
      }
}


 - (void)updateTimeAndProgressView
    {
        if ([self.player currentItem]) {
            CurrentArtistHeader *header = (CurrentArtistHeader *)[self.collectionView viewWithTag:50];
            float duration = CMTimeGetSeconds(self.player.currentItem.duration);
            float current = CMTimeGetSeconds(self.player.currentTime);
            header.progressView.progress = (current/duration);

            if (header.progressView.progress == 1) {
                [header.progressView setProgress:0.0 animated:YES];
            }
        }
    }