ImageScrollerView.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. //
  2. // ImageScrollerView.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 12/18/15.
  6. // Copyright © 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ImageScrollerView.h"
  9. #import "iSalesDB.h"
  10. #pragma mark -定义宏常量
  11. #define kImgViewCount 3
  12. #define kImgZoomScaleMin 1
  13. #define kImgZoomScaleMax 3
  14. #pragma mark -定义展示板
  15. #define kImgVLeft (UIImageView *)[_imgViewDic valueForKey:@"imgLeft"];
  16. #define kImgVCenter (UIImageView *)[_imgViewDic valueForKey:@"imgCenter"];
  17. #define kImgVRight (UIImageView *)[_imgViewDic valueForKey:@"imgRight"];
  18. @implementation ImageScrollerView
  19. {
  20. UIScrollView *_scrCenter;
  21. }
  22. - (id)initWithFrame:(CGRect)frame
  23. {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. }
  27. return self;
  28. }
  29. - (id)initWithFrame:(CGRect)frame withSourceData:(NSMutableArray *)imgSource withIndex:(NSInteger)index{
  30. self = [super initWithFrame:frame];
  31. if (self) {
  32. // 初始化控件属性
  33. [self initScrollView];
  34. // 设置数据源
  35. [self setImgSource:imgSource];
  36. // 设置图片下标
  37. [self setCurIndex:(int)index];
  38. }
  39. return self;
  40. }
  41. - (id)initWithFrame:(CGRect)frame withSourceURL:(NSMutableArray *)imgURL withIndex:(NSInteger)index
  42. {
  43. self = [super initWithFrame:frame];
  44. if (self) {
  45. NSMutableArray* data = [[NSMutableArray alloc]init];
  46. for(int i=0;i<imgURL.count;i++)
  47. {
  48. NSString* img_url = imgURL[i];
  49. NSString* file_name=[img_url lastPathComponent];
  50. NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  51. if(img_data!=nil)
  52. {
  53. dispatch_async(dispatch_get_main_queue(), ^{
  54. UIImage * img =[UIImage imageWithData:img_data];
  55. [data addObject:img];
  56. // if(i==count-1)
  57. // {
  58. //
  59. // }
  60. // if(i==0)
  61. // cell.imageView.image = img;
  62. });
  63. }
  64. else
  65. {
  66. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  67. dispatch_async(dispatch_get_main_queue(), ^{
  68. if(downloadimg_data!=nil)
  69. {
  70. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  71. UIImage * img =[UIImage imageWithData:downloadimg_data];
  72. [data addObject:img];
  73. // if(i==count-1)
  74. // {
  75. // UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:350];
  76. // if(qrimg!=nil)
  77. // [cell AddPhoto:qrimg];
  78. // }
  79. // if(i==0)
  80. // cell.imageView.image = img;
  81. }
  82. else
  83. {
  84. UIImage * img =[UIImage imageNamed:@"notfound_l"];
  85. [data addObject:img];
  86. // if(i==count-1)
  87. // {
  88. // UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:350];
  89. // if(qrimg!=nil)
  90. // [cell AddPhoto:qrimg];
  91. // }
  92. // if(i==0)
  93. // cell.imageView.image = img;
  94. }
  95. });
  96. }
  97. }
  98. // 初始化控件属性
  99. [self initScrollView];
  100. // 设置数据源
  101. [self setImgSource:data];
  102. // 设置图片下标
  103. [self setCurIndex:(int)index];
  104. }
  105. return self;
  106. }
  107. #pragma mark -初始化控件
  108. - (void)initScrollView{
  109. // 设置代理
  110. self.delegate = self;
  111. // 不显示滚动条
  112. self.showsHorizontalScrollIndicator = NO;
  113. self.showsVerticalScrollIndicator = NO;
  114. // 设置分页显示
  115. self.pagingEnabled = YES;
  116. //设置背景颜色
  117. self.backgroundColor = [UIColor clearColor];
  118. // 构建展示组
  119. [self initImgViewDic];
  120. }
  121. // 通过创建展示板
  122. - (UIImageView *)creatImageView{
  123. CGFloat width = self.frame.size.width;
  124. CGFloat height = self.frame.size.height;
  125. UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
  126. imgView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
  127. imgView.backgroundColor= [UIColor whiteColor];
  128. return imgView;
  129. }
  130. // 通过位置创建展示板
  131. - (UIScrollView *)scrollViewWithPosition:(MRImgLocation)imgLocation withImgView:(UIImageView *)imgView{
  132. CGFloat width = self.frame.size.width;
  133. CGFloat height = self.frame.size.height;
  134. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(width * imgLocation, 0, width, height)];
  135. scrollView.backgroundColor = [UIColor clearColor];
  136. [scrollView addSubview:imgView];
  137. // 设置图片显示样式
  138. imgView.contentMode = UIViewContentModeScaleAspectFit;
  139. return scrollView;
  140. }
  141. - (void)_initScrollCenter{
  142. // 设置缩放极限
  143. _scrCenter.maximumZoomScale = kImgZoomScaleMax;
  144. _scrCenter.minimumZoomScale = kImgZoomScaleMin;
  145. _scrCenter.delegate = self;
  146. }
  147. // 初始化展示板组
  148. - (void)initImgViewDic{
  149. UIImageView *imgLeft = [self creatImageView];
  150. UIImageView *imgCenter =[self creatImageView];
  151. UIImageView *imgRight = [self creatImageView];
  152. _imgViewDic = [[NSDictionary alloc] initWithObjectsAndKeys:
  153. imgLeft, @"imgLeft",
  154. imgCenter, @"imgCenter",
  155. imgRight, @"imgRight", nil];
  156. UIScrollView *scrLeft =
  157. [self scrollViewWithPosition:MRImgLocationLeft withImgView:imgLeft];
  158. _scrCenter =
  159. [self scrollViewWithPosition:MRImgLocationCenter withImgView:imgCenter];
  160. UIScrollView *scrRight =
  161. [self scrollViewWithPosition:MRImgLocationRight withImgView:imgRight];
  162. // 初始化scrCenter
  163. [self _initScrollCenter];
  164. // 添加双击手势
  165. UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapClick:)];
  166. doubleTap.numberOfTapsRequired = 2;
  167. [self addGestureRecognizer:doubleTap];
  168. // 放入展示板
  169. [self addSubview:scrLeft];
  170. [self addSubview:_scrCenter];
  171. [self addSubview:scrRight];
  172. }
  173. #pragma mark -setAttribute
  174. // 设置数据源
  175. - (void)setImgSource:(NSMutableArray *)imgSource{
  176. if (_imgSource != imgSource) {
  177. _imgSource = imgSource;
  178. // 设置展示板尺寸
  179. [self setConSize];
  180. }
  181. }
  182. // 展示板尺寸设置
  183. - (void)setConSize{
  184. CGSize size = self.frame.size;
  185. //设置内容视图的大小--单页填充、横向划动
  186. self.contentSize = CGSizeMake(size.width * kImgViewCount, size.height);
  187. // 设置显示页
  188. [self setContentOffset:CGPointMake(size.width, 0)];
  189. }
  190. - (void)setCurIndex:(int)curIndex{
  191. if (_imgSource.count > curIndex && curIndex >= 0) {
  192. _curIndex = curIndex;
  193. } else if (curIndex == -1){
  194. _curIndex = (int)_imgSource.count - 1;
  195. } else if (curIndex == _imgSource.count){
  196. _curIndex = 0;
  197. }
  198. // 判断数据类型 根据数据类型抉择使用情况
  199. // 如果需要使用URL,请导入延展
  200. Class urlCls = NSClassFromString(@"NSURL");
  201. if (_imgSource.count) {
  202. if ([_imgSource[0] isKindOfClass:urlCls]) {
  203. [self setAllImgVContentFromImage:[self imgListURLFromIndex:_curIndex]];
  204. }else{
  205. [self setAllImgVContentFromImage:[self imgListFromIndex:_curIndex]];
  206. }
  207. }
  208. }
  209. #pragma mark -功能函数
  210. // 根据当前索引通过URL赋值图片
  211. - (NSArray *)imgListURLFromIndex:(NSInteger)curIndex{
  212. long sCount = _imgSource.count;
  213. NSArray *imgList;
  214. NSURL *urlL;
  215. NSURL *urlC;
  216. NSURL *urlR;
  217. // 1、index在首个
  218. // L = last , C = first , R = C + 1
  219. // 2、index在中间
  220. // L = C - 1 , C = index , R = C + 1
  221. // 3、index在最后
  222. // L = C - 1 , C = last , R = first
  223. // count == 0
  224. // 不处理
  225. // count == 1
  226. // index在首个
  227. // count == 2
  228. // index在首个或最后
  229. // count > 2
  230. // 正常
  231. if (sCount) {
  232. // 首位
  233. if (curIndex == 0) {
  234. urlL = [[NSURL alloc] initWithString:[_imgSource lastObject]];
  235. urlC = [[NSURL alloc] initWithString:_imgSource[curIndex]];
  236. long nextIndex = curIndex == sCount - 1 ? curIndex : curIndex + 1;
  237. urlR = [[NSURL alloc] initWithString:_imgSource[nextIndex]];
  238. // 末位
  239. } else if (curIndex == sCount - 1){
  240. long lastIndex = curIndex == 0 ? curIndex : curIndex - 1;
  241. urlL = [[NSURL alloc] initWithString:_imgSource[lastIndex] ];
  242. urlC = [[NSURL alloc] initWithString:[_imgSource lastObject]];
  243. urlR = [[NSURL alloc] initWithString:_imgSource[0]];
  244. // 中间
  245. } else {
  246. urlL = [[NSURL alloc] initWithString:_imgSource[curIndex - 1] ];
  247. urlC = [[NSURL alloc] initWithString:_imgSource[curIndex]];
  248. urlR = [[NSURL alloc] initWithString:_imgSource[curIndex + 1]];
  249. }
  250. imgList = [[NSArray alloc] initWithObjects:urlL, urlC, urlR, nil];
  251. }
  252. return imgList;
  253. }
  254. // 根据当前索引赋值图片
  255. - (NSArray *)imgListFromIndex:(NSInteger)curIndex{
  256. long sCount = _imgSource.count;
  257. NSArray *imgList;
  258. UIImage *imgL;
  259. UIImage *imgC;
  260. UIImage *imgR;
  261. if (sCount) {
  262. // 首位
  263. if (curIndex == 0) {
  264. imgL = [_imgSource lastObject];
  265. imgC = _imgSource[curIndex];
  266. long nextIndex = curIndex == sCount - 1 ? curIndex : curIndex + 1;
  267. imgR = _imgSource[nextIndex];
  268. // 末位
  269. } else if (curIndex == sCount - 1){
  270. long lastIndex = curIndex == 0 ? curIndex : curIndex - 1;
  271. imgL = _imgSource[lastIndex] ;
  272. imgC = [_imgSource lastObject];
  273. imgR = _imgSource[0];
  274. // 中间
  275. } else {
  276. imgL = _imgSource[curIndex - 1];
  277. imgC = _imgSource[curIndex];
  278. imgR = _imgSource[curIndex + 1];
  279. }
  280. imgList = [[NSArray alloc] initWithObjects:imgL, imgC, imgR, nil];
  281. }
  282. return imgList;
  283. }
  284. // 载入一组图片
  285. - (void)setAllImgVContentFromImage:(NSArray *)imgList{
  286. // 将所有imgList中的数据载入展示板
  287. UIImageView *vLift = kImgVLeft;
  288. UIImageView *vCenter = kImgVCenter;
  289. UIImageView *vRight = kImgVRight;
  290. Class urlCls = NSClassFromString(@"NSURL");
  291. if (imgList.count) {
  292. if ([imgList[0] isKindOfClass:urlCls]) {
  293. // [vLift setImageWithURL:imgList[MRImgLocationLeft]];
  294. // [vCenter setImageWithURL:imgList[MRImgLocationCenter]];
  295. // [vRight setImageWithURL:imgList[MRImgLocationRight]];
  296. }else{
  297. [vLift setImage:imgList[MRImgLocationLeft]];
  298. [vCenter setImage:imgList[MRImgLocationCenter]];
  299. [vRight setImage:imgList[MRImgLocationRight]];
  300. }
  301. }
  302. }
  303. //// 谦让双击放大手势
  304. //- (void)requireDoubleGestureRecognizer:(UITapGestureRecognizer *)tep{
  305. // [tep requireGestureRecognizerToFail:[[self gestureRecognizers] lastObject]];
  306. //}
  307. #pragma mark -UIScrollViewDelegate
  308. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  309. {
  310. if (self == scrollView) {
  311. CGFloat width = self.frame.size.width;
  312. int currentOffset = scrollView.contentOffset.x/width - 1;
  313. [self setCurIndex:_curIndex + currentOffset];
  314. [scrollView setContentOffset:CGPointMake(width, 0) animated:NO];
  315. [_scrCenter setZoomScale:kImgZoomScaleMin];
  316. } else if (_scrCenter == scrollView)
  317. {
  318. CGFloat height = self.frame.size.height;
  319. CGFloat offsetY = _scrCenter.contentOffset.y;
  320. // 弹出判断
  321. if (height*1.8 < offsetY || offsetY < height*0.5) {
  322. [_scrCenter setZoomScale:kImgZoomScaleMin animated:YES];
  323. }
  324. }
  325. }
  326. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
  327. UIImageView *vCenter = kImgVCenter;
  328. return vCenter;
  329. }
  330. //#pragma mark -Tap手势处理
  331. - (void)doubleTapClick:(UITapGestureRecognizer *)tap{
  332. //判断当前放大的比例
  333. if (true /*_scrCenter.zoomScale > kImgZoomScaleMin*/) {
  334. //缩小
  335. [_scrCenter setZoomScale:kImgZoomScaleMin animated:YES];
  336. }
  337. // else{
  338. // //放大
  339. // [_scrCenter setZoomScale:kImgZoomScaleMax animated:YES];
  340. // }
  341. }
  342. /*
  343. // Only override drawRect: if you perform custom drawing.
  344. // An empty implementation adversely affects performance during animation.
  345. - (void)drawRect:(CGRect)rect {
  346. // Drawing code
  347. }
  348. */
  349. @end