这几天有点事多,都没时间摸电脑,几天不学习都不知道写代码了,话不多说,直接上代码,难免逻辑有bug,还请各位大神不要见笑,还望不吝赐教
程序欢迎界面我做的很不美,临时下载了几张图
首先创建两个文件welcomeVC和firstVC分别是启动页面和引导页面都继承自UIViewController
首先是welcomeVC.h
#import<UIKit/UIKit.h>
#define WIDTH (NSInteger)self.view.bounds.size.width
#define HEIGHT (NSInteger)self.view.bounds.size.height
@interface firstVC : UIViewController
@end
然后是welcomeVC.m
#import "welcomeVC.h"
#import "firstVC.h"
@interface welcomeVC ()
@property(nonatomic,strong)UIView*lunchV;
@property(nonatomic,strong)UIImageView*imageV;
@end
@implementation welcomeVC
- (void)viewDidLoad {
[super viewDidLoad];
_lunchV = [[UIView alloc] init];
self.lunchV.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
[self.view addSubview:_lunchV];
UIImageView*imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
UIImage*image = [UIImage imageNamed:@"1.JPG"];
imageV.image = image;
[self.lunchV addSubview:imageV];
[self.view bringSubviewToFront:self.lunchV];
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(removelunch) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:3.1 target:self selector:@selector(change) userInfo:nil repeats:NO];
}
-(void)removelunch
{
[self.lunchV removeFromSuperview];
}
- (void)change
{
self.view.window.rootViewController = [[firstVC alloc] init];
}
其中有些地方我自己不是很确定逻辑是否合理,还请不要见怪
firstVC.h
#import <UIKit/UIKit.h>
#define WIDTH (NSInteger)self.view.bounds.size.width
#define HEIGHT (NSInteger)self.view.bounds.size.height
@interface firstVC : UIViewController
@end
firstVC.m文件
#import "ViewController.h"
#import "firstVC.h"
@interface firstVC (){
// 创建页码控制器
UIPageControl *pageControl;
// 判断是否是第一次进入应用
BOOL flag;
}
@end
@implementation firstVC
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
for (int i=0; i<3; i++)
{
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"1-%d.jpg",i+1]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
// 在最后一页创建按钮
if (i == 2)
{
// 必须设置用户交互 否则按键无法操作
imageView.userInteractionEnabled = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(WIDTH / 3, HEIGHT * 7 / 8, WIDTH / 3, HEIGHT / 16);
[button setTitle:@"点击进入" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.layer.borderWidth = 2;
button.layer.cornerRadius = 5;
button.clipsToBounds = YES;
button.layer.borderColor = [UIColor whiteColor].CGColor;
[button addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:button];
}
imageView.image = image;
[myScrollView addSubview:imageView];
}
myScrollView.bounces = NO;
myScrollView.pagingEnabled = YES;
myScrollView.showsHorizontalScrollIndicator = NO;
myScrollView.contentSize = CGSizeMake(WIDTH * 3, HEIGHT);
myScrollView.delegate = self;
[self.view addSubview:myScrollView];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 3, HEIGHT * 15 / 16, WIDTH / 3, HEIGHT / 16)];
// 设置页数
pageControl.numberOfPages = 3;
// 设置页码的点的颜色
pageControl.pageIndicatorTintColor = [UIColor yellowColor];
// 设置当前页码的点颜色
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
[self.view addSubview:pageControl];
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// 计算当前在第几页
pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
}
// 点击按钮保存数据并切换根视图控制器
- (void) go:(UIButton *)sender{
flag = YES;
NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
// 保存用户数据
[useDef setBool:flag forKey:@"notFirst"];
[useDef synchronize];
// 切换根视图控制器
self.view.window.rootViewController = [[ViewController alloc] init];
}
AppDelegate.h文件不做变动
AppDelegate.m文件
#import "AppDelegate.h"
#import "welcomeVC.h"
#import "firstVC.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window makeKeyAndVisible];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
welcomeVC*root = [[welcomeVC alloc] init];
self.window.rootViewController = root;
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
NSLog(@"首次启动");
UIViewController *vc = [[UIViewController alloc] init];
self.window.rootViewController = vc;
}else {
NSLog(@"非首次启动");
welcomeVC *vc1 = [[welcomeVC alloc] init];
self.window.rootViewController = vc1;
}
return YES;
}
- (void)change
{
self.window.rootViewController = [[firstVC alloc] init];
}
ViewController.h和ViewController.m文件我没有写代码也就是主界面了
我自己搜了好多文档也浏览了不少博客,东拼西凑的总算达到了我要的效果,我知道其中有不合理的地方,还请各位大牛多多指点,或许是我自己学习方法不对吧,一个人看博客泡论坛,看视频,自学速度不是很快,但是还是有那么些许收获的,这就是对我的付出的最大回报,
看了很多大牛说,学习最快的办法就是输出,输出就是把书本的只是,把自己学到的知识,通过思考转化为自己的东西,最快的转化的方法就是拿出来和别人一起探讨,交流。所以在这里,对于这一篇东拼西凑才打到效果的文章,我就不写那些原著的大牛了,请各位朋友多多指教
网友评论
第一:NSUserDefaults中点击引导页按钮时存的key值,和AppDelegate中取的值明显不同,你居然说能实现效果,真是不可思议;
第二:welcomeVC中并没有对是否是第一次启动做判断,也就是说,只要加载了welcomeVC,后面就一定会加载firstVC;
第三:AppDelegate中change方法,我不知道是你没完全写出来,还是思路没理清,总之我没看到在哪里会调用它。
第四:welcomeVC真的有必要用两个定时器么?虽然我没有测试过,但是移除启动页视图和更换window根视图,感觉没什么冲突,完全没有必要把一个方法里能做的事分到两个方法里去。
代码之路遥遥无期,希望在这条路上都能互相帮助,共同进步。如果觉得我哪里说的不对,请指正。