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的功能,因此这里是最后一张图片的下一个图片的起始点(其实根本没有这个图片,因此这里会有点卡)。而不是说我门滑动以后,就只执行赋值的代码就不执行别的代码了。具体的优化和完整的功能实现,会后续再写。

相关文章
相关标签/搜索