2013-04-11 44 views
0

我需要你的幫助。我想創建一個水平ScrollView與一些AVPlayers。我試過這個ScrollView一些圖片,它的工作原理。現在的問題是,我可以滾動到View,但所有AVPlayerViews都處於相同位置,所以我只能看到最後加載的視頻和所有其他滾動位置都是空白的。UIScrollView與多個AVPlayerViews

這是我的類的源:

@implementation VPVideoScroller 

const CGFloat kScrollObjHeight = 748.0; 
const CGFloat kScrollObjWidth = 1024.0; 

NSUInteger numImages = 0; 

- (void)viewDidLoad 
{ 
    numImages = _pictureArray.count; 
    [super viewDidLoad]; 

    self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; 

    // 1. setup the scrollview for multiple images and add it to the view controller 
    self.scrollView.clipsToBounds = YES; 

    // load all the images from our bundle and add them to the scroll view 
    NSUInteger i; 
    for (i = 1; i <= numImages; i++) 
    { 

     if (i<3) { 

     /* 
     //------------ 
     MPMediaItem *item = [self.pictureArray objectAtIndex:(i-1)]; 

     NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL]; 
     */ 

     AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:self.currentURL]; 

     AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem]; 

     //------------ 
      UIView *imageView = [[UIView alloc] init]; 

     AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player]; 
     [player play]; 
     /* 
     UIImage *image = [self.pictureArray objectAtIndex:(i-1)]; 

     UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
     */ 

     // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 


     CGRect rect = imageView.frame; 
     rect.size.height = kScrollObjHeight; 
     rect.size.width = kScrollObjWidth; 
     imageView.frame = rect; 
     imageView.tag = i; // tag our images for later use when we place them in serial fashion 
     imageView.contentMode = UIViewContentModeScaleAspectFit; 

     [imageView.layer addSublayer:playerLayer]; 
     playerLayer.frame = imageView.layer.bounds; 

     [self.scrollView addSubview:imageView]; 
     } 
    } 

    [self layoutScrollImages]; // now place the photos in serial layout within the scrollview 
} 

- (void)layoutScrollImages 
{ 
    UIImageView *view = nil; 
    NSArray *subviews = [self.scrollView subviews]; 

    // reposition all image subviews in a horizontal serial fashion 
    CGFloat curXLoc = 0; 
    for (view in subviews) 
    { 
     if ([view isKindOfClass:[UIImageView class]] && view.tag > 0) 
     { 
      CGRect frame = view.frame; 
      frame.origin = CGPointMake(curXLoc, 0); 
      view.frame = frame; 

      curXLoc += (kScrollObjWidth); 
     } 
    } 

    // set the content size so it can be scrollable 
    //[self.scrollView setContentSize:CGSizeMake((numImages * kScrollObjWidth), [self.scrollView bounds].size.height)]; 

    [self.scrollView setContentSize:CGSizeMake((numImages * kScrollObjWidth), kScrollObjHeight)]; 


} 

pictureArray包含的URL的Array,但他們都是一樣的。所以AVPlayers應該顯示所有相同的視頻。

UIScrollView

預先感謝您。凱文

回答

0

我已經找到解決方案:)問題出在方法-(void)layoutScrollImages。當然,我不使用UIImageViews。這需要更改爲UIView,因爲AVPlayer玩家在UIVIew

此版本的方法正在工作。

- (void)layoutScrollImages 
{ 
    UIView *view = nil; 
    NSArray *subviews = [self.scrollView subviews]; 

    // reposition all image subviews in a horizontal serial fashion 
    CGFloat curXLoc = 0; 
    for (view in subviews) 
    { 
     if ([view isKindOfClass:[UIView class]] && view.tag > 0) 
     { 
      CGRect frame = view.frame; 
      frame.origin = CGPointMake(curXLoc, 0); 
      view.frame = frame; 

      curXLoc += (kScrollObjWidth); 
     } 
    } 

    // set the content size so it can be scrollable 
    //[self.scrollView setContentSize:CGSizeMake((numImages * kScrollObjWidth), [self.scrollView bounds].size.height)]; 

    [self.scrollView setContentSize:CGSizeMake((numImages * kScrollObjWidth), kScrollObjHeight)]; 

} 

我希望這也有助於其他人。