| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- //
- // ImageScrollerView.m
- // RedAnt ERP Mobile
- //
- // Created by Ray on 12/18/15.
- // Copyright © 2015 United Software Applications, Inc. All rights reserved.
- //
- #import "ImageScrollerView.h"
- #import "FileCache.h"
- #pragma mark -定义宏常量
- #define kImgViewCount 3
- #define kImgZoomScaleMin 1
- #define kImgZoomScaleMax 3
- #pragma mark -定义展示板
- #define kImgVLeft (UIImageView *)[_imgViewDic valueForKey:@"imgLeft"];
- #define kImgVCenter (UIImageView *)[_imgViewDic valueForKey:@"imgCenter"];
- #define kImgVRight (UIImageView *)[_imgViewDic valueForKey:@"imgRight"];
- @implementation ImageScrollerView
- {
- UIScrollView *_scrCenter;
- }
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
-
- }
- return self;
- }
- - (id)initWithFrame:(CGRect)frame withSourceData:(NSMutableArray *)imgSource withIndex:(NSInteger)index{
-
- self = [super initWithFrame:frame];
- if (self) {
-
- // 初始化控件属性
- [self initScrollView];
-
- // 设置数据源
- [self setImgSource:imgSource];
-
- // 设置图片下标
- [self setCurIndex:(int)index];
-
-
- }
- return self;
- }
- - (id)initWithFrame:(CGRect)frame withSourceURL:(NSMutableArray *)imgURL withIndex:(NSInteger)index
- {
- self = [super initWithFrame:frame];
- if (self) {
-
- NSMutableArray* data = [[NSMutableArray alloc]init];
- for(int i=0;i<imgURL.count;i++)
- {
- NSString* img_url = imgURL[i];
- NSString* file_name=[img_url lastPathComponent];
- NSData* img_data=[FileCache load_cached_img:file_name loadFrom:img_url];
- if(img_data!=nil)
- {
- dispatch_async(dispatch_get_main_queue(), ^{
- UIImage * img =[UIImage imageWithData:img_data];
- [data addObject:img];
-
-
- // if(i==count-1)
- // {
- //
- // }
- // if(i==0)
- // cell.imageView.image = img;
- });
- }
- else
- {
- NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
- dispatch_async(dispatch_get_main_queue(), ^{
-
-
-
- if(downloadimg_data!=nil)
- {
-
- [FileCache cache_img:downloadimg_data filename:file_name saveTo:img_url];
-
- UIImage * img =[UIImage imageWithData:downloadimg_data];
- [data addObject:img];
-
- // if(i==count-1)
- // {
- // UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:350];
- // if(qrimg!=nil)
- // [cell AddPhoto:qrimg];
- // }
- // if(i==0)
- // cell.imageView.image = img;
- }
- else
- {
-
-
- UIImage * img =[UIImage imageNamed:@"notfound_l"];
- [data addObject:img];
-
- // if(i==count-1)
- // {
- // UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:350];
- // if(qrimg!=nil)
- // [cell AddPhoto:qrimg];
- // }
- // if(i==0)
- // cell.imageView.image = img;
- }
-
- });
- }
- }
-
- // 初始化控件属性
- [self initScrollView];
-
- // 设置数据源
- [self setImgSource:data];
-
- // 设置图片下标
- [self setCurIndex:(int)index];
-
-
- }
- return self;
- }
- #pragma mark -初始化控件
- - (void)initScrollView{
-
- // 设置代理
- self.delegate = self;
-
- // 不显示滚动条
- self.showsHorizontalScrollIndicator = NO;
- self.showsVerticalScrollIndicator = NO;
-
- // 设置分页显示
- self.pagingEnabled = YES;
-
- //设置背景颜色
- self.backgroundColor = [UIColor clearColor];
-
- // 构建展示组
- [self initImgViewDic];
- }
- // 通过创建展示板
- - (UIImageView *)creatImageView{
-
- CGFloat width = self.frame.size.width;
- CGFloat height = self.frame.size.height;
-
- UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
- imgView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
- imgView.backgroundColor= [UIColor whiteColor];
- return imgView;
- }
- // 通过位置创建展示板
- - (UIScrollView *)scrollViewWithPosition:(MRImgLocation)imgLocation withImgView:(UIImageView *)imgView{
-
- CGFloat width = self.frame.size.width;
- CGFloat height = self.frame.size.height;
-
- UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(width * imgLocation, 0, width, height)];
-
- scrollView.backgroundColor = [UIColor clearColor];
-
- [scrollView addSubview:imgView];
-
- // 设置图片显示样式
- imgView.contentMode = UIViewContentModeScaleAspectFit;
-
- return scrollView;
- }
- - (void)_initScrollCenter{
- // 设置缩放极限
- _scrCenter.maximumZoomScale = kImgZoomScaleMax;
- _scrCenter.minimumZoomScale = kImgZoomScaleMin;
-
- _scrCenter.delegate = self;
- }
- // 初始化展示板组
- - (void)initImgViewDic{
-
- UIImageView *imgLeft = [self creatImageView];
- UIImageView *imgCenter =[self creatImageView];
- UIImageView *imgRight = [self creatImageView];
-
- _imgViewDic = [[NSDictionary alloc] initWithObjectsAndKeys:
- imgLeft, @"imgLeft",
- imgCenter, @"imgCenter",
- imgRight, @"imgRight", nil];
-
- UIScrollView *scrLeft =
- [self scrollViewWithPosition:MRImgLocationLeft withImgView:imgLeft];
- _scrCenter =
- [self scrollViewWithPosition:MRImgLocationCenter withImgView:imgCenter];
- UIScrollView *scrRight =
- [self scrollViewWithPosition:MRImgLocationRight withImgView:imgRight];
-
- // 初始化scrCenter
- [self _initScrollCenter];
-
- // 添加双击手势
- UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapClick:)];
- doubleTap.numberOfTapsRequired = 2;
-
- [self addGestureRecognizer:doubleTap];
-
- // 放入展示板
- [self addSubview:scrLeft];
- [self addSubview:_scrCenter];
- [self addSubview:scrRight];
-
-
- }
- #pragma mark -setAttribute
- // 设置数据源
- - (void)setImgSource:(NSMutableArray *)imgSource{
-
- if (_imgSource != imgSource) {
- _imgSource = imgSource;
-
- // 设置展示板尺寸
- [self setConSize];
- }
- }
- // 展示板尺寸设置
- - (void)setConSize{
-
- CGSize size = self.frame.size;
-
- //设置内容视图的大小--单页填充、横向划动
- self.contentSize = CGSizeMake(size.width * kImgViewCount, size.height);
-
- // 设置显示页
- [self setContentOffset:CGPointMake(size.width, 0)];
- }
- - (void)setCurIndex:(int)curIndex{
-
- if (_imgSource.count > curIndex && curIndex >= 0) {
- _curIndex = curIndex;
- } else if (curIndex == -1){
- _curIndex = (int)_imgSource.count - 1;
- } else if (curIndex == _imgSource.count){
- _curIndex = 0;
- }
-
- // 判断数据类型 根据数据类型抉择使用情况
- // 如果需要使用URL,请导入延展
- Class urlCls = NSClassFromString(@"NSURL");
-
- if (_imgSource.count) {
- if ([_imgSource[0] isKindOfClass:urlCls]) {
- [self setAllImgVContentFromImage:[self imgListURLFromIndex:_curIndex]];
- }else{
- [self setAllImgVContentFromImage:[self imgListFromIndex:_curIndex]];
- }
- }
- }
- #pragma mark -功能函数
- // 根据当前索引通过URL赋值图片
- - (NSArray *)imgListURLFromIndex:(NSInteger)curIndex{
-
- long sCount = _imgSource.count;
- NSArray *imgList;
- NSURL *urlL;
- NSURL *urlC;
- NSURL *urlR;
-
-
- // 1、index在首个
- // L = last , C = first , R = C + 1
- // 2、index在中间
- // L = C - 1 , C = index , R = C + 1
- // 3、index在最后
- // L = C - 1 , C = last , R = first
-
- // count == 0
- // 不处理
- // count == 1
- // index在首个
- // count == 2
- // index在首个或最后
- // count > 2
- // 正常
-
- if (sCount) {
-
- // 首位
- if (curIndex == 0) {
- urlL = [[NSURL alloc] initWithString:[_imgSource lastObject]];
- urlC = [[NSURL alloc] initWithString:_imgSource[curIndex]];
- long nextIndex = curIndex == sCount - 1 ? curIndex : curIndex + 1;
- urlR = [[NSURL alloc] initWithString:_imgSource[nextIndex]];
- // 末位
- } else if (curIndex == sCount - 1){
- long lastIndex = curIndex == 0 ? curIndex : curIndex - 1;
- urlL = [[NSURL alloc] initWithString:_imgSource[lastIndex] ];
- urlC = [[NSURL alloc] initWithString:[_imgSource lastObject]];
- urlR = [[NSURL alloc] initWithString:_imgSource[0]];
- // 中间
- } else {
- urlL = [[NSURL alloc] initWithString:_imgSource[curIndex - 1] ];
- urlC = [[NSURL alloc] initWithString:_imgSource[curIndex]];
- urlR = [[NSURL alloc] initWithString:_imgSource[curIndex + 1]];
- }
-
- imgList = [[NSArray alloc] initWithObjects:urlL, urlC, urlR, nil];
- }
-
- return imgList;
- }
- // 根据当前索引赋值图片
- - (NSArray *)imgListFromIndex:(NSInteger)curIndex{
-
- long sCount = _imgSource.count;
- NSArray *imgList;
-
- UIImage *imgL;
- UIImage *imgC;
- UIImage *imgR;
-
- if (sCount) {
-
- // 首位
- if (curIndex == 0) {
- imgL = [_imgSource lastObject];
- imgC = _imgSource[curIndex];
- long nextIndex = curIndex == sCount - 1 ? curIndex : curIndex + 1;
- imgR = _imgSource[nextIndex];
- // 末位
- } else if (curIndex == sCount - 1){
- long lastIndex = curIndex == 0 ? curIndex : curIndex - 1;
- imgL = _imgSource[lastIndex] ;
- imgC = [_imgSource lastObject];
- imgR = _imgSource[0];
- // 中间
- } else {
- imgL = _imgSource[curIndex - 1];
- imgC = _imgSource[curIndex];
- imgR = _imgSource[curIndex + 1];
- }
-
- imgList = [[NSArray alloc] initWithObjects:imgL, imgC, imgR, nil];
- }
-
- return imgList;
- }
- // 载入一组图片
- - (void)setAllImgVContentFromImage:(NSArray *)imgList{
-
- // 将所有imgList中的数据载入展示板
- UIImageView *vLift = kImgVLeft;
- UIImageView *vCenter = kImgVCenter;
- UIImageView *vRight = kImgVRight;
-
- Class urlCls = NSClassFromString(@"NSURL");
-
- if (imgList.count) {
- if ([imgList[0] isKindOfClass:urlCls]) {
- // [vLift setImageWithURL:imgList[MRImgLocationLeft]];
- // [vCenter setImageWithURL:imgList[MRImgLocationCenter]];
- // [vRight setImageWithURL:imgList[MRImgLocationRight]];
- }else{
- [vLift setImage:imgList[MRImgLocationLeft]];
- [vCenter setImage:imgList[MRImgLocationCenter]];
- [vRight setImage:imgList[MRImgLocationRight]];
- }
- }
-
- }
- //// 谦让双击放大手势
- //- (void)requireDoubleGestureRecognizer:(UITapGestureRecognizer *)tep{
- // [tep requireGestureRecognizerToFail:[[self gestureRecognizers] lastObject]];
- //}
- #pragma mark -UIScrollViewDelegate
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
- {
- if (self == scrollView) {
- CGFloat width = self.frame.size.width;
- int currentOffset = scrollView.contentOffset.x/width - 1;
- [self setCurIndex:_curIndex + currentOffset];
- [scrollView setContentOffset:CGPointMake(width, 0) animated:NO];
- [_scrCenter setZoomScale:kImgZoomScaleMin];
- } else if (_scrCenter == scrollView)
- {
- CGFloat height = self.frame.size.height;
- CGFloat offsetY = _scrCenter.contentOffset.y;
-
- // 弹出判断
- if (height*1.8 < offsetY || offsetY < height*0.5) {
- [_scrCenter setZoomScale:kImgZoomScaleMin animated:YES];
- }
- }
- }
- - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
- UIImageView *vCenter = kImgVCenter;
- return vCenter;
- }
- //#pragma mark -Tap手势处理
- - (void)doubleTapClick:(UITapGestureRecognizer *)tap{
- //判断当前放大的比例
- if (true /*_scrCenter.zoomScale > kImgZoomScaleMin*/) {
- //缩小
- [_scrCenter setZoomScale:kImgZoomScaleMin animated:YES];
- }
- // else{
- // //放大
- // [_scrCenter setZoomScale:kImgZoomScaleMax animated:YES];
- // }
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|