UIScrollView 节省空间
来源:互联网 发布:手机淘宝地址在哪改 编辑:程序博客网 时间:2024/05/05 13:35
//// 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, HEIGHT); [_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
0 0
- UIScrollView 节省空间
- 节省空间
- Redis节省空间
- [数据结构]如何节省程序空间
- proto如何做到节省空间。
- 联合:节省空间的类
- 创建竖表节省空间
- Ucenter Flickr相册插件 节省主机空间
- 用匿名联合来节省空间
- 求空间节省比例的方法
- 360 清理系统垃圾,节省负数空间
- 移动设计空间节省之道
- 【编程珠玑】第十章 节省空间
- 编程珠玑--节省空间
- 移动设计空间节省之道
- 巧用备份数节省hdfs空间
- 二进制文件一定比文本文件节省空间吗?
- C/C++语言里面的空间节省
- 39、微信通讯录联系人快速索引侧边栏SideBar
- Java中的static关键字解析
- c++的类在jsb中的内存管理
- 数据结构与算法基础(二)之线性表的顺序存储
- 原生js实现ajax
- UIScrollView 节省空间
- 风口的猪-中国牛市
- linux系统分区文件系统划分札记
- 两个数组的合并
- CodeForces 148D Bag of Mice 概率DP
- 23种设计模式(14):合成模式
- android程序中Zxing二维码扫描图片变形 问题解决方法
- #import与@class的区别
- android实现EditText中加多行下划线的一种方法