ImageScrollerView.m 10 KB

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