制作引导页[3]
制作引導頁[3]
第三種方法是將整個引導頁寫到一個controller中,是通用性最高的一種寫法:)
效果:
源碼:
AppDelegate.m
WelcomeViewController.m
// // WelcomeViewController.m // Show // // Copyright (c) 2014年 Y.X. All rights reserved. //#import "WelcomeViewController.h" #import "RootViewController.h"@interface WelcomeViewController ()@end@implementation WelcomeViewController- (void)viewDidLoad {[super viewDidLoad]; }- (void)viewDidAppear:(BOOL)animated {[self scrollView]; }- (void)scrollView {CGRect rect = self.view.window.bounds;CGFloat width = rect.size.width;CGFloat height = rect.size.height;// 初始化scrollViewUIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:rect];scrollView.pagingEnabled = YES;scrollView.tag = 0x77;scrollView.contentSize = CGSizeMake(width * 3, height);// 添加一些控件for (int i = 0; i < 3; i++){UIView *tmp = [[UIView alloc] initWithFrame:CGRectMake(i*width, 0, width, height)];tmp.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.fgreen:arc4random()%255/255.fblue:arc4random()%255/255.falpha:1];if (i == 2){YXButton *button = [[YXButton alloc] initWithFrame:CGRectMake(0, 0, 140, 30)];button.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin"size:20.f];button.layer.cornerRadius = 3.f;[button addTarget:selfaction:@selector(buttonEvent:)forControlEvents:UIControlEventTouchUpInside];[button setBackgroundColor:[UIColor blackColor]highlightedBackgroundColor:[UIColor whiteColor]];[button setNormalTitleColor:[UIColor whiteColor]highlightedTitleColor:[UIColor blackColor]disabledTitleColor:nil];[button setNormalTitle:@"YouXianMing"highlightedTitle:@"YouXianMing"disabledTitle:@"YouXianMing"];button.center = self.view.window.center;[tmp addSubview:button];}[scrollView addSubview:tmp];}// 添加到UIWindow當中[self.view.window addSubview:scrollView]; }- (void)buttonEvent:(UIButton *)button {UIScrollView *scrollView = (UIScrollView *)[self.view.window viewWithTag:0x77];scrollView.userInteractionEnabled = NO;// 動畫[UIView animateWithDuration:2.0 animations:^{scrollView.alpha = 0.f;} completion:^(BOOL finished) {// 從UIWindow上移除這個scrollView[scrollView removeFromSuperview];// 切換視圖控制器self.view.window.rootViewController = [RootViewController new];}]; }@end
RootViewController.m
// // RootViewController.m // Show // // Copyright (c) 2014年 Y.X. All rights reserved. //#import "RootViewController.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor whiteColor];[UIView animateWithDuration:2 animations:^{self.view.backgroundColor = [UIColor blackColor];}]; }@end
幾個需要注意的地方:
動畫結束后要切換視圖控制器
過渡更自然
?
總結:
這幾篇教程制作引導頁的核心都是在UIWindow上加載視圖或者視圖控制器,就是這么容易哦:)
總結
- 上一篇: angular2初入眼帘之-多compo
- 下一篇: [设计模式篇]工厂模式和抽象工厂模式