- 1.引入MQGuideView文件捺氢,根據(jù)需要修改引導(dǎo)頁(yè)數(shù)量和圖片名稱
static const NSInteger NumberOfGuide = 3; //引導(dǎo)頁(yè)數(shù)
//修改圖片名稱
imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"default_%d",i+1]];
- 2.在首頁(yè)進(jìn)行登錄判斷藻丢,是否是第一次打開(kāi)App,并存入本地摄乒。初始化MQGuideView悠反,加到當(dāng)前視圖頁(yè)面,具體在哪里添加隨個(gè)人喜好缺狠,這里只做簡(jiǎn)單演示问慎。
- (void)viewWillAppear:(BOOL)animated{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstLaunch"]) {
_isFirstLaunch = YES;
[[NSUserDefaults standardUserDefaults] setBool:_isFirstLaunch forKey:@"isFirstLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];
_guideView = [[MQGuideView alloc]initWithFrame:[UIScreen mainScreen].bounds];
_guideView.delegate = self;
[self.view addSubview:_guideView];
}
}
- 3.添加代理事件,來(lái)控制引導(dǎo)頁(yè)的跳轉(zhuǎn)挤茄,這里做了個(gè)簡(jiǎn)單的動(dòng)畫(huà)
- (void)onPassButtonPressed{
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_guideView.alpha = 0;
} completion:^(BOOL finished) {
[_guideView removeFromSuperview];
}];
}
下面是.h和.m文件
.h文件
@protocol MQGuideViewDelegate <NSObject>
//代理方法 跳過(guò)引導(dǎo)頁(yè)
-(void)onPassButtonPressed;
@end
@interface MQGuideView : UIView
@property (nonatomic,weak) id<MQGuideViewDelegate> delegate;
@end
.m文件
@interface MQGuideView () <UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *pageControl;
@end
@implementation MQGuideView
static const NSInteger NumberOfGuide = 3; //引導(dǎo)頁(yè)數(shù)
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, screenW, screenH)];
[self addSubview:scrollView];
UIButton *passBtn = [[UIButton alloc]initWithFrame:CGRectMake(screenW-70.0f, 40.0f, 50.0f, 30.0f)];
[passBtn addTarget:self action:@selector(onFinishedIntroButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[passBtn setImage:[UIImage imageNamed:@"default_pass"] forState:UIControlStateNormal];
[self addSubview:passBtn];
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.contentSize = CGSizeMake(screenW*NumberOfGuide, screenH);
scrollView.pagingEnabled = YES;
for (int i=0; i<NumberOfGuide; i++) {
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(i*screenW, 0, screenW, screenH)];
imgView.contentMode = UIViewContentModeScaleAspectFit;
imgView.userInteractionEnabled = YES;
//修改圖片名稱
imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"default_%d",i+1]];
[scrollView addSubview:imgView];
if (i == (NumberOfGuide-1)) {
UIButton *startBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.center.x, self.frame.size.height*.8, 150.0f, 40.0f)];
startBtn.center = CGPointMake(self.center.x, self.frame.size.height*.8);
startBtn.layer.cornerRadius = 5.0f;
startBtn.layer.masksToBounds = YES;
startBtn.titleLabel.font = [UIFont systemFontOfSize:17.0f];
startBtn.backgroundColor = [UIColor brownColor];
[startBtn setTitle:@"立即開(kāi)啟" forState:UIControlStateNormal];
[startBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[startBtn addTarget:self action:@selector(onFinishedIntroButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[imgView addSubview:startBtn];
}
}
_pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(screenW/2-40.0f, screenH-40.0f, 80.0f, 10.0f)];
[self addSubview:_pageControl];
_pageControl.enabled = YES;
_pageControl.numberOfPages = NumberOfGuide;
_pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
_pageControl.currentPageIndicatorTintColor = [UIColor redColor];
}
return self;
}
#pragma mark - 跳過(guò)引導(dǎo)代理
- (void)onFinishedIntroButtonPressed{
[self.delegate onPassButtonPressed];
}
#pragma mark - 滑動(dòng)代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat scrollViewW = scrollView.width;
CGFloat x = scrollView.contentOffset.x;
int page = (x + scrollViewW/2)/scrollViewW;
_pageControl.currentPage = page;
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者