輪播引導頁

輪播引導頁


輪播引導頁早已成為主流的引導方式,下面加單介紹一下實現方式。


界面預覽:

GuidePages.gif

代碼簡介:
主要知識點:
UIScrollView 基礎控件的使用

    (instancetype)init
    {
return [self initWithImageDatas:nil completion:nil];
    }

    // init with imageDatas and completion     - (instancetype)initWithImageDatas:(NSArray *)imageDatas completion:(void (^)(void))buttonAction
    {
self = [super init];
if (self)
{
    [self initView];
    //因為使用了懶加載,_imageDatas = imageDatas不會調用initContentView
    [self setImageDatas:imageDatas];
    _buttonAction = buttonAction;
}
return self;
    }

    //懶加載,并初始化內容- (void)setImageDatas:(NSArray *)imageDatas
    {
_imageDatas = imageDatas;
[self initContentView];
    }

    //基礎視圖初始化- (void)initView
    {
// init view
self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

// init scrollView
_scrollView = [[UIScrollView alloc] init];
_scrollView.delegate = self;
_scrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
_scrollView.backgroundColor = [UIColor clearColor];
_scrollView.bounces = NO;
_scrollView.pagingEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
[self addSubview:_scrollView];

// init pageControl
_pageControl =
    [[UIPageControl alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 30, SCREEN_WIDTH, 10)];
_pageControl.currentPage = 0;
_pageControl.hidesForSinglePage = YES;
_pageControl.pageIndicatorTintColor = [UIColor grayColor];
_pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
[self addSubview:_pageControl];

// init button
_actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    }

    //指定數據后,初始化顯示內容- (void)initContentView
   {
if (_imageDatas.count)
{
    _pageControl.numberOfPages = _imageDatas.count;
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * _imageDatas.count, SCREEN_HEIGHT);
    for (int i = 0; i < _imageDatas.count; i++)
    {
        NSString *imageName = _imageDatas[i];
        UIImageView *imgView =
            [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imgView.frame = CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [self.scrollView addSubview:imgView];

        if (i == _imageDatas.count - 1)
        {
            _actionButton.frame =
                CGRectMake(SCREEN_WIDTH / 2 - 70, SCREEN_HEIGHT - 70, 140, 35);
            _actionButton.layer.cornerRadius = 5;
            _actionButton.layer.masksToBounds = YES;
            [_actionButton setTitle:@"進  入" forState:UIControlStateNormal];
            _actionButton.tintColor = [UIColor whiteColor];
            _actionButton.backgroundColor = [UIColor redColor];
            [_actionButton addTarget:self
                              action:@selector(enterButtonClick)
                    forControlEvents:UIControlEventTouchUpInside];
            [imgView addSubview:_actionButton];
            //設置可以響應交互,UIImageView的默認值為NO
            imgView.userInteractionEnabled = YES;
        }
    }
}
   }- (void)enterButtonClick
   {
if (_buttonAction)
{
    _buttonAction();
}
   }- (void)scrollViewDidScroll:(UIScrollView *)scrollView
   {
_pageControl.currentPage = (_scrollView.contentOffset.x + SCREEN_WIDTH / 2) / SCREEN_WIDTH;
    }

下載地址:
http://pan.baidu.com/s/1c24pBwO

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,595評論 25 708
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,259評論 4 61
  • 出來近四年時間,每到假期回家都有一種日漸冷清的感覺。北方的冬天,樹上的葉子落得毛都不剩,村子后面海拔三十米的小山包...
    青杏兒閱讀 183評論 0 1
  • 六月的天,七月的雨,不只都是陰天和霉雨。 拖著疲憊的身子,趕了數千公里的火車。一路都是花生瓜子礦泉水,八寶粥碗面火...
    不將就生活閱讀 227評論 0 1
  • 王建軍 我是一名教師,一名語文教師,但從內心中,我是懼怕這一稱謂的。在有些場合,當陌生人問及我的職業,我有些惶恐地...
    東營王建軍閱讀 943評論 20 21