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
原创粉丝点击