輪播引導(dǎo)頁
輪播引導(dǎo)頁早已成為主流的引導(dǎo)方式,下面加單介紹一下實現(xiàn)方式代芜。
界面預(yù)覽:
GuidePages.gif
代碼簡介:
主要知識點:
UIScrollView 基礎(chǔ)控件的使用
(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不會調(diào)用initContentView
[self setImageDatas:imageDatas];
_buttonAction = buttonAction;
}
return self;
}
//懶加載,并初始化內(nèi)容- (void)setImageDatas:(NSArray *)imageDatas
{
_imageDatas = imageDatas;
[self initContentView];
}
//基礎(chǔ)視圖初始化- (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];
}
//指定數(shù)據(jù)后侣诵,初始化顯示內(nèi)容- (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:@"進(jìn) 入" forState:UIControlStateNormal];
_actionButton.tintColor = [UIColor whiteColor];
_actionButton.backgroundColor = [UIColor redColor];
[_actionButton addTarget:self
action:@selector(enterButtonClick)
forControlEvents:UIControlEventTouchUpInside];
[imgView addSubview:_actionButton];
//設(shè)置可以響應(yīng)交互殴穴,UIImageView的默認(rèn)值為NO
imgView.userInteractionEnabled = YES;
}
}
}
}- (void)enterButtonClick
{
if (_buttonAction)
{
_buttonAction();
}
}- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
_pageControl.currentPage = (_scrollView.contentOffset.x + SCREEN_WIDTH / 2) / SCREEN_WIDTH;
}