程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> scrollView實現無縫鏈接滾動視圖

scrollView實現無縫鏈接滾動視圖

編輯:C++入門知識

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";
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved