xcode7怎么给app添加启动页
1个回答
展开全部
此处 是自己封装了一个独立的引导页,在任何项目中均可独立引用
首先看.h
[objc] view plain copy
<span style="color:#666666;">#import <UIKit/UIKit.h>
/**
* App首次开机引导页
*/
@interface GuideView : UIScrollView<UIScrollViewDelegate>
@end</span><span style="color:#ff4647;">
</span>
接着.m
[objc] view plain copy
#import "GuideView.h"
/**
* 引导页张数
*/
#define DEF_GUIDE_COUNT 3
@implementation GuideView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.bounces=NO;
self.contentSize = CGSizeMake(DEF_WIDTH(self)*DEF_GUIDE_COUNT, DEF_HEIGHT(self));
self.backgroundColor = [UIColor blackColor];
self.showsHorizontalScrollIndicator = NO;
self.pagingEnabled = YES;
self.delegate=self;
self.backgroundColor = [UIColor clearColor];
for (int i=0; i<DEF_GUIDE_COUNT; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(DEF_SCREEN_WIDTH*i, 0, DEF_SCREEN_WIDTH,DEF_SCREEN_HEIGHT)];
[imageView setBackgroundColor:[UIColor redColor]];
[imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Guide%d",i]]];
if ([UIScreen mainScreen].bounds.size.height == 480) {
[imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Guide%d-480",i]]];
}
[self addSubview:imageView];
if (i==2) {
UIButton*button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
button.alpha = 0.5;
[button addTarget:self action:@selector(beginClick) forControlEvents:UIControlEventTouchUpInside];
imageView.userInteractionEnabled = YES;
[imageView addSubview:button];
}
}
}
return self;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x>DEF_SCREEN_WIDTH*3+20) {
[UIView animateWithDuration:0.3 animations:^{
self.alpha = 0.0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
}
#pragma mark - 点击事件
- (void)beginClick
{
self.userInteractionEnabled = NO;
[UIView animateWithDuration:0.3 animations:^{
self.alpha = 0.0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
引导页上 有的是点击的,有的是需要滑动进入主页的,这两个根据自己的需求来,例子中这两种情况都满足,具体的还得根据自己的需求来,
使用方法也十分简单
[objc] view plain copy
//引导页
if (![DEF_PERSISTENT_GET_OBJECT(@"showGuide") boolValue])
{
[self makeui];
DEF_PERSISTENT_SET_OBJECT([NSNumber numberWithBool:YES], @"showGuide");
GuideView *guide = [[GuideView alloc] initWithFrame:self.window.bounds];
[self.window addSubview:guide];
}
/**
*
取出永久存储的对象
*
* @param key 所需对象对应的key
* @return key所对应的对象
*/
#define DEF_PERSISTENT_GET_OBJECT(key) [[NSUserDefaults standardUserDefaults] objectForKey:key]
这是我自己定义的一个宏 用来本地化的
首先看.h
[objc] view plain copy
<span style="color:#666666;">#import <UIKit/UIKit.h>
/**
* App首次开机引导页
*/
@interface GuideView : UIScrollView<UIScrollViewDelegate>
@end</span><span style="color:#ff4647;">
</span>
接着.m
[objc] view plain copy
#import "GuideView.h"
/**
* 引导页张数
*/
#define DEF_GUIDE_COUNT 3
@implementation GuideView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.bounces=NO;
self.contentSize = CGSizeMake(DEF_WIDTH(self)*DEF_GUIDE_COUNT, DEF_HEIGHT(self));
self.backgroundColor = [UIColor blackColor];
self.showsHorizontalScrollIndicator = NO;
self.pagingEnabled = YES;
self.delegate=self;
self.backgroundColor = [UIColor clearColor];
for (int i=0; i<DEF_GUIDE_COUNT; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(DEF_SCREEN_WIDTH*i, 0, DEF_SCREEN_WIDTH,DEF_SCREEN_HEIGHT)];
[imageView setBackgroundColor:[UIColor redColor]];
[imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Guide%d",i]]];
if ([UIScreen mainScreen].bounds.size.height == 480) {
[imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Guide%d-480",i]]];
}
[self addSubview:imageView];
if (i==2) {
UIButton*button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
button.alpha = 0.5;
[button addTarget:self action:@selector(beginClick) forControlEvents:UIControlEventTouchUpInside];
imageView.userInteractionEnabled = YES;
[imageView addSubview:button];
}
}
}
return self;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x>DEF_SCREEN_WIDTH*3+20) {
[UIView animateWithDuration:0.3 animations:^{
self.alpha = 0.0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
}
#pragma mark - 点击事件
- (void)beginClick
{
self.userInteractionEnabled = NO;
[UIView animateWithDuration:0.3 animations:^{
self.alpha = 0.0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
引导页上 有的是点击的,有的是需要滑动进入主页的,这两个根据自己的需求来,例子中这两种情况都满足,具体的还得根据自己的需求来,
使用方法也十分简单
[objc] view plain copy
//引导页
if (![DEF_PERSISTENT_GET_OBJECT(@"showGuide") boolValue])
{
[self makeui];
DEF_PERSISTENT_SET_OBJECT([NSNumber numberWithBool:YES], @"showGuide");
GuideView *guide = [[GuideView alloc] initWithFrame:self.window.bounds];
[self.window addSubview:guide];
}
/**
*
取出永久存储的对象
*
* @param key 所需对象对应的key
* @return key所对应的对象
*/
#define DEF_PERSISTENT_GET_OBJECT(key) [[NSUserDefaults standardUserDefaults] objectForKey:key]
这是我自己定义的一个宏 用来本地化的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询