scrollView实现无缝链接滚动视图

源码下载

代码实现:

-(void)createView
{
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0 + 64, 320, 230)];
    self.scrollView.backgroundColor = [UIColor yellowColor];
    //  锁定滑动方向
    _scrollView.directionalLockEnabled = NO;
    _scrollView.contentSize = CGSizeMake(320 * (number + 2) , 240);

    //  弹跳效果
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    //  scrollView 上面添加imageView
    for (int i = 0; i < number + 2; i ++) {
        self.testImageView = [[UIImageView alloc] initWithFrame:CGRectMake(320 * i + 3, 3 , 314 , 224)];
        _testImageView.userInteractionEnabled = YES;
        _testImageView.backgroundColor = [UIColor redColor];
        _testImageView.tag = 100 + i;
        [self.scrollView addSubview:_testImageView];
    }
    //初始偏移量,显示第一张图片
    _scrollView.contentOffset = CGPointMake(320, 0);
    [self.view addSubview:_scrollView];
    //添加图片
    [self addImage];
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(90, 270, 250, 15)];
    self.pageControl.numberOfPages = number;
    _pageControl.backgroundColor= [UIColor clearColor];
    //  页码控制点颜色
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor cyanColor];
    
    //  pageControl 添加目标事件
    [self.pageControl addTarget:self action:@selector(handlePageControlAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.pageControl];
    [self addTimer];

    self.YJFImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 235 + 64, 320, 568 - 240 - 64)];
    self.YJFImageView.image = LOADIMAGE(@"背景图", @"jpg");
    [self.view addSubview:_YJFImageView];
    UILabel *YJFLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 60)];
    YJFLabel.text = @"Email:[email protected]";
    YJFLabel.textColor = [UIColor blackColor];
    YJFLabel.textAlignment = NSTextAlignmentCenter;
    YJFLabel.font = [UIFont systemFontOfSize:16];
    [self.YJFImageView addSubview:YJFLabel];
}

-(void)addImage
{
    for (int i = 0; i < number; i++) {
        UIImageView *picImageView = (UIImageView *)[self.view viewWithTag:101+i];
        picImageView.image = LOADIMAGE(self.imageNameArray[i], @"jpg");
    }
    UIImageView *picImageViewFirst = (UIImageView *)[self.scrollView viewWithTag:100 + number + 1];
    picImageViewFirst.image = LOADIMAGE(self.imageNameArray[number - 1], @"jpg");
    
    UIImageView *picImageViewLast = (UIImageView *)[self.scrollView viewWithTag:100 + number + 1];
    picImageViewLast.image = LOADIMAGE(self.imageNameArray[0], @"jpg");

}

//  pageControl 点击相应事件
- (void)handlePageControlAction:(UIPageControl *)sender
{
    NSInteger currentPage = _pageControl.currentPage;
    CGPoint offsetPoint = CGPointMake(currentPage * 320, 0);
    //  根据当前currentPage 滚动scrollView
    [self.scrollView setContentOffset:offsetPoint animated:YES];
}

//  添加定时器, 实现轮播图
- (void)addTimer
{
    [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(runTimer) userInfo:nil repeats:YES];
}

//  定时器响应方法
- (void)runTimer
{
    CGFloat x = _scrollView.contentOffset.x;
    [_scrollView setContentOffset:CGPointMake(x + 320, 0) animated:YES];
}

#pragma mark - ScrollView Delegate
//让pageControl随着视图变化
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (0 == scrollView.contentOffset.x) {
        [scrollView setContentOffset:CGPointMake(320 * number, 0) animated:NO];
    } else if (((number + 1) * 320) == scrollView.contentOffset.x) {
        [scrollView setContentOffset:CGPointMake(320, 0) animated:NO];
    }
    self.pageControl.currentPage = scrollView.contentOffset.x / 320 - 1;
}

-(void)customizedNavigationBar
{
    self.navigationItem.title = @"轮播图demo";
}


你可能感兴趣的:(循环滚动,无缝链接)