您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS UIPageControl 與 UIScrollView 搭配實作分頁輪播

編輯:iOS 開發綜合

以下示範如何用 UIScrollView 加上 UIPageControl 做出一組可循環的分頁輪播(圖片請自行加入專案)。自動播放的部分待補充。

一、頭檔宣告

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    NSMutableArray *slideImages;
    UIPageControl *_page;
}
@end

二、實作內容

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 240)];
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.contentOffset = CGPointMake(320, 0);
    _scrollView.contentSize = CGSizeMake(1920, 240);
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.userInteractionEnabled = YES;
    [self.view addSubview:_scrollView];

    slideImages = [[NSMutableArray alloc] initWithObjects:@"1.png", @"2.png", @"3.png", @"4.png", nil];

    // 把最後一張放到第一位,作為向左滑的入口
    UIImageView *imageView = [[UIImageView alloc]
        initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count] - 1)]]];
    imageView.frame = CGRectMake(0, 0, 320, 240);
    [_scrollView addSubview:imageView];

    // 依序鋪上真正的內容
    for (int i = 0; i < [slideImages count]; i++) {
        UIImageView *imageView = [[UIImageView alloc]
            initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];
        imageView.frame = CGRectMake(320 * i + 320, 0, 320, 240);
        imageView.userInteractionEnabled = YES;
        [_scrollView addSubview:imageView];
    }

    // 把第一張複製到最尾端,作為向右滑的出口
    imageView = [[UIImageView alloc]
        initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0]]];
    imageView.frame = CGRectMake(320 * 5, 0, 320, 240);
    [_scrollView addSubview:imageView];

    // pageControl
    _page = [[UIPageControl alloc] initWithFrame:CGRectMake(240, 230, 70, 30)];
    _page.numberOfPages = 4;
    _page.currentPage = 0;
    [_page addTarget:self action:@selector(pageAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_page];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int currentPage = (_scrollView.contentOffset.x - _scrollView.frame.size.width / ([slideImages count] + 2))
                        / _scrollView.frame.size.width + 1;
    NSLog(@"%d", currentPage);

    if (currentPage == 0) {
        // 滑到複製的尾巴,無聲跳回真正的第一張
        [_scrollView scrollRectToVisible:CGRectMake(320 * 4, 0, 320, 240) animated:NO];
    } else if (currentPage == ([slideImages count] + 1)) {
        // 滑到複製的頭部,無聲跳回真正的最後一張
        [_scrollView scrollRectToVisible:CGRectMake(320, 0, 320, 240) animated:NO];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    int page = _scrollView.contentOffset.x / 320 - 1;
    _page.currentPage = page;
}

#pragma mark - pageControl 點擊

- (void)pageAction {
    int page = _page.currentPage;
    [_scrollView setContentOffset:CGPointMake(320 * (page + 1), 0)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

三、原理拆解

整個循環的關鍵是「首尾各複製一張」:scrollView 裡實際排列為 [最後一張、1、2、3、4、第一張],起始偏移固定在 320,也就是真正的第一張。

當使用者往左滑到那張複製的「最後一張」時,scrollViewDidEndDecelerating 會把位置無聲地拉回真正的 4;往右滑過頭時則拉回真正的 1。因為兩張圖在視覺上完全相同,切換瞬間察覺不到。

要補上自動播放的話,加一個 NSTimer 每 3 秒呼叫 setContentOffset 前進一頁即可,但記得在 scrollViewWillBeginDragging 時暫停、DidEndDecelerating 後恢復,避免使用者操作到一半被強制跳頁。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved