HBImageViewList.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // HBImageViewList.m
  3. // MyTest
  4. //
  5. // Created by weqia on 13-7-31.
  6. // Copyright (c) 2013年 weqia. All rights reserved.
  7. //
  8. #import "HBImageViewList.h"
  9. @implementation HBImageViewList
  10. @synthesize imageViews=_imageViews;
  11. #pragma -mark 覆盖父类的方法
  12. - (id)initWithFrame:(CGRect)frame
  13. {
  14. self = [super initWithFrame:frame];
  15. if (self) {
  16. // Initialization code
  17. self.backgroundColor=[UIColor blackColor];
  18. self.pagingEnabled=YES;
  19. self.delegate=self;
  20. _prePage=0;
  21. _tapOnceAction=nil;
  22. _target=nil;
  23. self.showsHorizontalScrollIndicator=NO;
  24. self.showsVerticalScrollIndicator=NO;
  25. _pageControl=[[UIPageControl alloc]initWithFrame:CGRectZero];
  26. _pageControl.pageIndicatorTintColor=[UIColor lightGrayColor];
  27. _pageControl.currentPageIndicatorTintColor=[UIColor whiteColor];
  28. _pageControl.hidesForSinglePage=YES;
  29. [_pageControl addTarget:self action:@selector(pageChangeAction) forControlEvents:UIControlEventValueChanged];
  30. }
  31. return self;
  32. }
  33. -(void)didMoveToSuperview
  34. {
  35. [_pageControl sizeToFit];
  36. CGRect frame=_pageControl.frame;
  37. frame.origin.x=(self.frame.size.width-frame.size.width)/2;
  38. frame.origin.y= JX_SCREEN_HEIGHT - 100;
  39. _pageControl.frame=frame;
  40. [self.superview addSubview:_pageControl];
  41. }
  42. -(void)didMoveToWindow
  43. {
  44. [_pageControl removeFromSuperview];
  45. }
  46. #pragma -mark 接口方法
  47. -(void)addImages:(NSArray *)images
  48. {
  49. _images=images;
  50. _imageViews=[NSMutableArray array];
  51. NSInteger count=[_images count];
  52. self.contentSize=CGSizeMake(self.frame.size.width*count, self.frame.size.height);
  53. for(int i=0;i<count;i++)
  54. {
  55. HBImageScroller * scroll=[[HBImageScroller alloc]initWithFrame:CGRectMake(self.frame.size.width*i,0 , self.bounds.size.width,self.bounds.size.height)];
  56. [self addSubview:scroll];
  57. [scroll setImage:[_images objectAtIndex:i]];
  58. [_imageViews addObject:scroll];
  59. [scroll addTarget:self tapOnceAction:@selector(tapImageAction:)];
  60. }
  61. _pageControl.numberOfPages=count;
  62. }
  63. -(void)addImagesURL:(NSArray*)urls withSmallImage:(NSArray*)images
  64. {
  65. _imageViews=[NSMutableArray array];
  66. NSInteger count=[urls count];
  67. self.contentSize=CGSizeMake(self.frame.size.width*count, self.frame.size.height);
  68. for(int i=0;i<count;i++)
  69. {
  70. HBImageScroller * scroll=[[HBImageScroller alloc]initWithFrame:CGRectMake(self.frame.size.width*i,0 , self.bounds.size.width,self.bounds.size.height)];
  71. [self addSubview:scroll];
  72. UIImage *image= nil;
  73. if(i<[images count])
  74. image=[images objectAtIndex:i];
  75. ObjUrlData* p = [urls objectAtIndex:i];
  76. [scroll setImageWithURL:p.url andSmallImage:image];
  77. p = nil;
  78. [_imageViews addObject:scroll];
  79. [scroll addTarget:self tapOnceAction:@selector(tapImageAction:)];
  80. }
  81. _pageControl.numberOfPages=count;
  82. }
  83. -(void)setImage:(UIImage *)image
  84. {
  85. int offset=0;
  86. NSInteger count= [_images count];
  87. for(int i=0;i<count;i++)
  88. if([_images objectAtIndex:i]==image)
  89. {
  90. offset=i;
  91. break;
  92. }
  93. self.contentOffset=CGPointMake(JX_SCREEN_WIDTH*offset, 0);
  94. _prePage=offset;
  95. _pageControl.currentPage=offset;
  96. }
  97. -(void)addTarget:(id)target tapOnceAction:(SEL)action
  98. {
  99. _target=target;
  100. _tapOnceAction=action;
  101. }
  102. -(void)addImagesURL:(NSArray *)urls
  103. {
  104. [self addImagesURL:urls withSmallImage:nil];
  105. }
  106. -(void)setIndex:(int) index
  107. {
  108. self.contentOffset=CGPointMake(JX_SCREEN_WIDTH*index, 0);
  109. _prePage=index;
  110. _pageControl.currentPage=index;
  111. }
  112. #pragma -mark 实现委托方法
  113. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  114. {
  115. int page= scrollView.contentOffset.x/self.frame.size.width;
  116. if(_prePage!=page)
  117. {
  118. HBImageScroller* scroll=[ _imageViews objectAtIndex:_prePage];
  119. [scroll reset];
  120. }
  121. _prePage=page;
  122. _pageControl.currentPage=page;
  123. }
  124. #pragma -mark 事件响应方法
  125. -(void)tapImageAction:(UIImageView*)view
  126. {
  127. if(_tapOnceAction&&_target&&[_target respondsToSelector:_tapOnceAction])
  128. #pragma clang diagnostic push
  129. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  130. [_target performSelector:_tapOnceAction withObject:view];
  131. #pragma clang diagnostic pop
  132. }
  133. -(void) pageChangeAction
  134. {
  135. NSInteger page=_pageControl.currentPage;
  136. [self scrollRectToVisible:CGRectMake(page*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height) animated:YES];
  137. }
  138. @end