首先创建继承于UIView的GuidePageView.h和GuidePageView.m
// GuidePageView.h
// ProgramGuidePage
#import <UIKit/UIKit.h>
@interface GuidePageView : UIView
- (id)initWithImages:(NSArray *)images;
@end
// GuidePageView.m
// ProgramGuidePage
#define kKEY_WINDOW [[UIApplication sharedApplication]keyWindow]
#import "GuidePageView.h"
@interface GuidePageView ()
@property (nonatomic, retain) UIScrollView * grideScroll;
@end
@implementation GuidePageView
- (id)initWithImages:(NSArray *)images
{
self = [superinitWithFrame:kKEY_WINDOW.bounds];
if (self)
{
[kKEY_WINDOW addSubview:self];
self.grideScroll = [[UIScrollView alloc] initWithFrame:self.bounds];
self.grideScroll.contentSize = CGSizeMake(kKEY_WINDOW.frame.size.width *images.count, kKEY_WINDOW.frame.size.height);
self.grideScroll.bounces = NO;
self.grideScroll.pagingEnabled= YES;
[selfaddSubview:self.grideScroll];
[self.grideScroll release];
[selfloadImagesWithArray:images];
}
return self;
}
- (void)loadImagesWithArray:(NSArray *)items
{
for (int i =0; i < items.count; i++)
{
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * kKEY_WINDOW.frame.size.width, 0,kKEY_WINDOW.frame.size.width, kKEY_WINDOW.frame.size.height)];
imageView.userInteractionEnabled = YES;
imageView.image = [UIImage imageNamed:[items objectAtIndex:i]];
[self.grideScroll addSubview:imageView];
if (i ==items.count - 1) {
UIButton * but = [UIButton buttonWithType:UIButtonTypeSystem];
but.frame = CGRectMake(120,
kKEY_WINDOW.frame.size.height - 100,
kKEY_WINDOW.frame.size.width - 240,
40);
but.layer.cornerRadius = 4;
but.backgroundColor = [UIColor grayColor];
[butsetTitle:@"进入体验" forState:UIControlStateNormal];
[butaddTarget:self action:@selector(beginExperience:)forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:but];
}
}
}
- (void)beginExperience:(UIButton *)sender
{
[UIViewanimateWithDuration:.3
animations:^{
self.frame = CGRectMake(self.frame.origin.x,
[UIScreen mainScreen].bounds.size.height,
self.frame.size.width,
self.frame.size.height);
}
completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
@end
在AppDelegate.m里引入头文件#import "GuidePageView.h",写入一下代码,用将引导面所需要的图片写入数组内:
if (![[NSUserDefaults standardUserDefaults]boolForKey:@"firstRun"]) {
NSArray*array = @[@"one.jpg",
@"two.jpg",
@"three.jpg",
@"four.jpg"];
GuidePageView *guide = [[GuidePageView alloc]initWithImages:array];
[[NSUserDefaults standardUserDefaults] setBool:YESforKey:@"firstRun"];
}