UIScrollView 节省空间
2016-02-25 16:23
351 查看
// // ViewController.m // MG货仓 // // Created by FC on 16/1/26. // Copyright © 2016年 fchl. All rights reserved. // #define WIDTH [UIScreen mainScreen].bounds.size.width #define HEIGHT [UIScreen mainScreen].bounds.size.height #import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate> { UICollectionView *_collectionView; UIScrollView *_scrollView; UIView *_firstView; UIView *_secondView; UIView *_thirdView; UIColor *_color[5]; int _count; int _index; int _currentType; BOOL _isDecelerating; } - (void)createIndexView; - (void)createFirstView; - (void)createLastView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; _count = 5; _index = 0; _currentType = 0; _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)]; _scrollView.delegate = self; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.showsVerticalScrollIndicator = NO; _scrollView.directionalLockEnabled = YES; _scrollView.pagingEnabled = YES; _color[0] = [UIColor redColor]; _color[1] = [UIColor greenColor]; _color[2] = [UIColor blueColor]; _color[3] = [UIColor cyanColor]; _color[4] = [UIColor yellowColor]; [self.view addSubview:_scrollView]; [self createFirstView]; } -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ CGFloat pageWidth = scrollView.frame.size.width; int pageIndex = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; switch (_currentType) { case 0: { _index = _index + pageIndex; if (pageIndex == 1) { [self createIndexView]; } break; } case 1: { _index = _index + pageIndex - 1; if (pageIndex == 0) { [self createIndexView]; } break; } case 2: { _index = _index + pageIndex - 1; if (_index == 0) { [self createFirstView]; }else if (_index == _count - 1){ [self createLastView]; }else{ [self createIndexView]; } break; } default: break; } NSLog(@"_index = %d",_index); scrollView.userInteractionEnabled = YES; } - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView { scrollView.userInteractionEnabled = NO; } - (void)createIndexView { [_firstView removeFromSuperview]; [_secondView removeFromSuperview]; [_thirdView removeFromSuperview]; _firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, WIDTH, HEIGHT)]; [_scrollView addSubview:_firstView]; _firstView.backgroundColor = _color[_index -1]; _secondView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH, 50, WIDTH, HEIGHT)]; [_scrollView addSubview:_secondView]; _secondView.backgroundColor = _color[_index]; _thirdView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH*2, 50, WIDTH, HEIGHT)]; [_scrollView addSubview:_thirdView]; _thirdView.backgroundColor = _color[_index + 1]; [_scrollView scrollRectToVisible:CGRectMake(WIDTH, 50, WIDTH, HEIGHT) animated:NO]; _scrollView.contentSize = CGSizeMake(WIDTH*3, HEIGHT); _currentType = 2; } - (void)createLastView { [_firstView removeFromSuperview]; [_secondView removeFromSuperview]; [_thirdView removeFromSuperview]; _firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, WIDTH, HEIGHT)]; [_scrollView addSubview:_firstView]; _firstView.backgroundColor = _color[_index - 1]; _secondView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH, 50, WIDTH, HEIGHT)]; [_scrollView addSubview:_secondView]; _secondView.backgroundColor = _color[_index]; _scrollView.contentSize = CGSizeMake(WIDTH*2, H 9d93 EIGHT); [_scrollView scrollRectToVisible:CGRectMake(WIDTH, 50, WIDTH, HEIGHT) animated:NO]; _currentType = 1; } - (void)createFirstView { [_firstView removeFromSuperview]; [_secondView removeFromSuperview]; [_thirdView removeFromSuperview]; _firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, WIDTH, HEIGHT)]; [_scrollView addSubview:_firstView]; _firstView.backgroundColor = _color[_index]; _secondView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH, 50, WIDTH, HEIGHT)]; [_scrollView addSubview:_secondView]; _secondView.backgroundColor = _color[_index + 1]; _scrollView.contentSize = CGSizeMake(WIDTH*2, HEIGHT); [_scrollView scrollRectToVisible:CGRectMake(0, 50, WIDTH, HEIGHT) animated:NO]; _currentType = 0; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- UI用塑料泡沫设计草图
- easyui datagrid上传服务器参数page改名
- hdu 4027 Can you answer these queries?
- iOS initWithFrame:frame] 与 [UIButton buttonWithType] 对比
- CodeForces 626 A. Robot Sequence(水~)
- Android uiautomator环境搭建
- angular 服务(factory service constant value provider)学习笔记
- Divide and Conquer
- 1101 Quick Sort
- UIImageView用法
- 浅谈 easyui tabs 的href和content属性
- java request获取各种数据
- incredibuild 命令行 error D8037
- 用数据库连接池解决org.hibernate.exception.JDBCConnectionException:could not execute query
- Ultra-QuickSort(树状数组求逆序对数)
- IOS之UITabBarController
- 基于pt-query-digest的慢查询日志持续追踪 推荐
- 从源代码安装Guidance
- ./configure: error: the GeoIP module requires the GeoIP library.
- 《iOS Human Interface Guidelines》——The Status Bar