iOS优化内存的横向ScrollView
来源:互联网 发布:塞班s60v5 软件下载 编辑:程序博客网 时间:2024/06/07 18:23
原理就是ScrollView中同时只有1、2、3张图片,每次翻动结束后,重新设置这三张图片,如1、2、3,原来是2,向右翻动结束减速后将1、2、3换成2、3、4,由于是结束滚动后替换的,所以看上去跟平滑滚动是一样的
#import "ViewController.h"@implementation ViewController{ UIScrollView *_mainScrollView; NSMutableArray *_imagePathArray; NSInteger _currentIndex; CGFloat _width; CGFloat _height;}- (void)viewDidLoad{ [super viewDidLoad]; _width = self.view.frame.size.width; _height = self.view.frame.size.height; self.view.backgroundColor = [UIColor whiteColor]; _mainScrollView = [[UIScrollView alloc] init]; _mainScrollView.frame = CGRectMake(0, 0, _width , _height); _mainScrollView.delegate = self; _mainScrollView.pagingEnabled = YES; _mainScrollView.bounces = NO; _mainScrollView.showsHorizontalScrollIndicator = NO; _mainScrollView.contentSize = CGSizeMake(_width*3, _height); [self.view addSubview:_mainScrollView]; _imagePathArray = [[NSMutableArray alloc] init]; for(int i=1;i<7;i++) { NSString *path = [NSString stringWithFormat:@"%@/%d.jpg",[[NSBundle mainBundle] resourcePath],i]; [_imagePathArray addObject:path]; } _currentIndex = 0; [self loadImageView];}- (void)loadImageView{ // 移除之前加载的 for(UIView *view in _mainScrollView.subviews) { [view removeFromSuperview]; } // 当前页 UIImage *currentImage = [[UIImage alloc] initWithContentsOfFile:[_imagePathArray objectAtIndex:_currentIndex]]; UIImageView *currentImageView = [[UIImageView alloc] initWithImage:currentImage]; currentImageView.frame = CGRectMake(_width, 0, _width, _height); [_mainScrollView addSubview:currentImageView]; // 左侧页 UIImage *preImage = [[UIImage alloc] initWithContentsOfFile:_currentIndex-1<0?[_imagePathArray lastObject]:[_imagePathArray objectAtIndex:_currentIndex-1]]; UIImageView *preImageView = [[UIImageView alloc] initWithImage:preImage]; preImageView.frame = CGRectMake(0, 0, _width, _height); [_mainScrollView addSubview:preImageView]; // 右侧页 UIImage *nextImage = [[UIImage alloc] initWithContentsOfFile:_currentIndex+1==_imagePathArray.count?[_imagePathArray firstObject]:[_imagePathArray objectAtIndex:_currentIndex+1]]; UIImageView *nextImageView = [[UIImageView alloc] initWithImage:nextImage]; nextImageView.frame = CGRectMake(_width*2, 0, _width, _height); [_mainScrollView addSubview:nextImageView]; [_mainScrollView setContentOffset:CGPointMake(_width, 0)];}//翻页- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ NSLog(@"scrollView.contentOffset.x = %.0f",scrollView.contentOffset.x); int index = scrollView.contentOffset.x/_width; if(index == 0) { _currentIndex = _currentIndex-1<0?_imagePathArray.count-1:_currentIndex-1; [self loadImageView]; } if(index == 2) { _currentIndex = _currentIndex+1==_imagePathArray.count?0:_currentIndex+1; [self loadImageView]; }}@end
0 0
- iOS优化内存的横向ScrollView
- 横向滑动的 ScrollView
- 自定义横向的ScrollView
- ios开发scrollview不能横向移动
- scrollView 横向
- iOS横向滚动的scrollView和系统pop手势返回冲突的解决办法
- 自定义HorizontalScrollView(横向滑动的ScrollView)
- 给ScrollView添加一个横向的进度条
- ios内存优化的轮播图
- IOS(swift)-scrollView(tableView) ·图片加载逻辑的优化
- 解决ScrollView嵌套RecyclerView(横向)或ListView(横向)时,横向滑动不顺畅的问题。
- Android菜单横向ScrollView
- 自定义横向的ScrollView的开发和用途
- 横向滑动和纵向滑动互不受影响的ScrollView
- android scrollview嵌套webview横向滚动冲突的问题
- iOS scrollview的实现
- 模仿ios的scrollView
- 关于IOS 的内存有优化问题
- Java反射
- Csharp: read excel file using Open XML SDK 2.5
- Csharp: read excel file using Open XML SDK 2.5
- 使用java8轻松处理日期
- CentOS系统下的Hadoop集群(第5期副刊)_JDK和SSH无密码配置
- iOS优化内存的横向ScrollView
- Janus the Architect
- 第一次写博客,学会分享,乐于奉献
- 谷歌刚发布了一款时尚“垃圾桶”
- 【Java】Eclipse最实用的快捷键(持续更新)
- Flex 中文字体终极解决方案
- Architects’ Focus Is on the Boundaries and Interfaces
- 写在前面的话
- 素数打表