在RootView中 - (void)addAllViews { // 1. 创建UIScrollView对象,并设置大小 self.scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, self.bounds.size.width - 40, self.bounds.size.height - 40)] autorelease]; // 2. 添加 [self addSubview:_scrollView]; // 3. 设置背景色 _scrollView.backgroundColor = [UIColor cyanColor];
// 4. 设置内容视图大小 _scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width * 10, _scrollView.bounds.size.height);
// 5. 添加图片 for (int i = 1; i < 11; i++) { // 5.1 遍历出图片的名称 NSString *imageName = [NSString stringWithFormat:@"%d.png", i]; // 5.2 获取图片数据 UIImage *image = [UIImage imageNamed:imageName]; // 5.3 将图片数据添加到UIImageView上 UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; // 5.4 设置imageView的位置和大小 imageView.frame = CGRectMake(_scrollView.bounds.size.width * (i - 1), 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height); // 5.5 添加到scrollView上 [_scrollView addSubview:imageView]; // 5.6 release一次 [imageView release];
// 6. 设置scrollView整页滑动 _scrollView.pagingEnabled = YES;
// 7. 设置滚动条的隐藏 _scrollView.showsHorizontalScrollIndicator = NO;
// 8. 设置scrollView的弹动效果 _scrollView.bounces = NO;
// 添加小点点 self.pageControl = [[[UIPageControl alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 28, self.bounds.size.width, 30)] autorelease]; _pageControl.numberOfPages = 10; _pageControl.pageIndicatorTintColor = [UIColor greenColor]; _pageControl.currentPageIndicatorTintColor = [UIColor redColor]; [self addSubview:_pageControl];
// 添加进入按钮 self.enterButton = [UIButton buttonWithType:UIButtonTypeSystem]; [_enterButton setTitle:@"进入" forState:UIControlStateNormal]; _enterButton.frame = CGRectMake((self.bounds.size.width - 100) / 2, self.bounds.size.height - 60, 100, 35); _enterButton.backgroundColor = [UIColor whiteColor]; [self addSubview:_enterButton]; _enterButton.hidden = YES; // 隐藏按钮
}
// // RootViewController.m // Lesson_07 // // Created by lanou3g on 14-12-8. // Copyright (c) 2014年 蓝鸥科技. All rights reserved. // 在根视图控制器中 #import "RootViewController.h" #import "RootView.h" #import "HomeViewController.h" @interface RootViewController () <UIScrollViewDelegate> #pragma mark 声明私有属性 @property (nonatomic, retain) RootView *rootView; @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)loadView { self.rootView = [[[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease]; self.view = _rootView; } - (void)viewDidLoad { [super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
// 设置scrollView的代理 _rootView.scrollView.delegate = self;
// 去给pageControl添加点击事件 [_rootView.pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
// 给enterButton按钮绑定事件 [_rootView.enterButton addTarget:self action:@selector(enterButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } #pragma mark - 实现进入按钮的事件 - (void)enterButtonAction:(UIButton *)sender { HomeViewController *homeVC = [HomeViewController new]; // 设置模态视图跳转的动画效果 homeVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; // 模态跳转 [self presentViewController:homeVC animated:YES completion:nil]; [homeVC release];
// 保存为已经进入的状态 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"kFirstEnterFlag"]; [[NSUserDefaults standardUserDefaults] synchronize]; // 即时保存,不等到按Home键的时候采取保存内容 } #pragma mark - 实现小点点的监听事件 - (void)pageControlAction:(UIPageControl *)sender { // 1. 获取当前点的页面 NSInteger index = sender.currentPage; // 2. 计算出偏移量 CGPoint offsetPoint = CGPointMake(index * _rootView.scrollView.bounds.size.width, 0); // 3. 将偏移量赋值给scrollView // _rootView.scrollView.contentOffset = offsetPoint; [_rootView.scrollView setContentOffset:offsetPoint animated:YES];
// 判断并且决定是否显示进入按钮 if (index == 9) { _rootView.enterButton.hidden = NO; } else { _rootView.enterButton.hidden = YES; }
} #pragma mark - UIScrollViewDelegate Methos #pragma mark 正在滑动 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { } #pragma mark 结束拖拽 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { } #pragma mark 结束自由滚动 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { // 判断是第几页 NSInteger index = scrollView.contentOffset.x / scrollView.bounds.size.width; NSLog(@"%ld", index + 1);
// 停止滑动后,修改小点点的位置 _rootView.pageControl.currentPage = index;
// 判断并且决定是否显示进入按钮 if (index == 9) { _rootView.enterButton.hidden = NO; } else { _rootView.enterButton.hidden = YES; }
} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)dealloc { [_rootView release];
[super dealloc]; } @end |
|