0

我需要你的帮助。我想ScrollView用一些AVPlayers. 我ScrollView用一些图片试过这个,它有效。现在的问题是我可以滚动View,但是所有的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包含一个ArrayURL,但它们都是相同的。所以AVPlayers应该显示所有相同的视频。

UIScrollView

先感谢您。凯文

4

1 回答 1

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)];

}

我希望这对其他人也有帮助。

于 2013-04-11T10:05:41.840 回答