码迷,mamicode.com
首页 > 其他好文 > 详细

scrollView实现无缝链接滚动视图

时间:2014-10-28 15:34:38      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:无缝链接 循环滚动

源码下载

代码实现:

-(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:summer2014mht@sina.com";
    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";
}


scrollView实现无缝链接滚动视图

标签:无缝链接 循环滚动

原文地址:http://blog.csdn.net/mhtios/article/details/40539917

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!