scrollView循環播放器(一)

/**優化

 *  設置scrollViewspa

 */3d

- (void)setUpScrollView:(NSArray *)array {代理

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];orm

    scrollView.delegate = self;圖片

    scrollView.pagingEnabled = YES;it

    scrollView.showsHorizontalScrollIndicator = NO;scroll

    scrollView.showsVerticalScrollIndicator = NO;方法

    [self addSubview:scrollView];im

    self.scrollView = scrollView;

}


/**

 *  設置scrollView的滾動圖片

 */

- (void)setUpImage:(NSArray *)array {

    CGSize contentSize;

    CGPoint startPoint;

    

        for (int i = 0; i < array.count; i++) {

            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];

            imageView.image = [UIImage imageNamed:array[i]];

            [self.scrollView addSubview:imageView];

            contentSize = CGSizeMake(array.count * self.frame.size.width, 0);

            startPoint = CGPointZero;

        }

    

    self.scrollView.contentSize = contentSize;

}


#pragma mark - scrollView代理方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    NSLog(@"scrollViewDidScroll");

    

    if (scrollView.contentOffset.x < 0) {

        NSLog(@"%lf",scrollView.contentOffset.x);

        NSLog(@"scrollView.contentOffset.x < self.frame.size.width");

        [self.scrollView setContentOffset:CGPointMake(self.frame.size.width * self.imageArray.count, 0) animated:NO];

    }

    

    NSLog(@"%f",scrollView.contentOffset.x);

}

  

demo只實現向右滑動,不斷播放的效果。來講一下,這段代碼。[self.scrollView setContentOffset:CGPointMake(self.frame.size.width * self.imageArray.count0animated:NO];這裏爲何是 self.imageArray.count 而不是self.imageArray.count - 1呢,由於當咱們滑動,這裏從新賦值以後,還會執行向右滑動整個page的功能,因此這裏是最後一張圖片的下一個圖片的起始點(其實根本沒有這個圖片,因此這裏會有點卡)。而不是說我門滑動以後,就只執行賦值的代碼就不執行別的代碼了。具體的優化和完整的功能實現,會後續再寫。

相關文章
相關標籤/搜索