gifViewController.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #import "gifViewController.h"
  2. #import "SCGIFImageView.h"
  3. #define BEGIN_FLAG @"["
  4. #define END_FLAG @"]"
  5. @implementation gifViewController
  6. @synthesize delegate=_delegate,faceArray,imageArray;
  7. - (id)initWithFrame:(CGRect)frame {
  8. self = [super initWithFrame:frame];
  9. // self.backgroundColor = [UIColor darkGrayColor];
  10. // self.backgroundColor = HEXCOLOR(0xF2F2F2);
  11. self.backgroundColor = [UIColor whiteColor];
  12. imageArray = [[NSMutableArray alloc] init];
  13. faceArray = [[NSMutableArray alloc] init];
  14. margin = 18;
  15. // tempN = (JX_SCREEN_WIDTH <= 320) ? 8:10;
  16. tempN = JX_SCREEN_WIDTH / (60 + margin);
  17. if (((tempN + 1) * 60 + tempN * margin) <= JX_SCREEN_WIDTH) {
  18. tempN += 1;
  19. }
  20. [self getGifFiles];
  21. [self create];
  22. return self;
  23. }
  24. -(void)getGifFiles{
  25. NSString* dir = gifImageFilePath;
  26. NSString* Path;
  27. NSString* ext;
  28. NSArray *contentOfFolder = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dir error:NULL];
  29. for (NSString *aPath in contentOfFolder) {
  30. Path = [dir stringByAppendingPathComponent:aPath];
  31. ext = [aPath pathExtension];
  32. BOOL isDir;
  33. if ([[NSFileManager defaultManager] fileExistsAtPath:Path isDirectory:&isDir] && !isDir)
  34. {
  35. if( [ext isEqualToString:@"gif"] ){
  36. SCGIFImageView* iv = [[SCGIFImageView alloc] initWithGIFFile:Path];
  37. [imageArray addObject:[iv getFrameAsImageAtIndex:0]];
  38. [faceArray addObject:[Path lastPathComponent]];
  39. // [iv release];
  40. }
  41. }
  42. }
  43. int n = fmod([faceArray count], (tempN * 2));
  44. maxPage = [faceArray count]/(tempN*2);
  45. if(n != 0)
  46. maxPage++;
  47. }
  48. - (void)dealloc {
  49. // [faceArray release];
  50. // [imageArray release];
  51. // [_gifIv release];
  52. // [super dealloc];
  53. }
  54. -(void)create{
  55. _sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-20)];
  56. _sv.contentSize = CGSizeMake(WIDTH_PAGE*maxPage, self.frame.size.height-20);
  57. _sv.pagingEnabled = YES;
  58. _sv.scrollEnabled = YES;
  59. _sv.delegate = self;
  60. _sv.showsVerticalScrollIndicator = NO;
  61. _sv.showsHorizontalScrollIndicator = NO;
  62. _sv.userInteractionEnabled = YES;
  63. _sv.minimumZoomScale = 1;
  64. _sv.maximumZoomScale = 1;
  65. _sv.decelerationRate = 0.01f;
  66. _sv.backgroundColor = [UIColor clearColor];
  67. [self addSubview:_sv];
  68. // [_sv release];
  69. int n = 0;
  70. int startX = (JX_SCREEN_WIDTH - tempN * 60 - (tempN - 1) * margin) / 2;
  71. for(int i=0;i<maxPage;i++){
  72. int x=WIDTH_PAGE*i + startX,y=0;
  73. for(int j=0;j<tempN * 2;j++){
  74. if(n>=[faceArray count])
  75. break;
  76. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  77. button.frame = CGRectMake(x, y+10, 60, 60);
  78. button.tag = n;
  79. [button setBackgroundImage:[imageArray objectAtIndex:n] forState:UIControlStateNormal];
  80. [button addTarget:self action:@selector(actionSelect:)forControlEvents:UIControlEventTouchUpInside];
  81. [_sv addSubview:button];
  82. if ((j + 1) % tempN == 0) {
  83. x = WIDTH_PAGE*i + startX;
  84. y += 70;
  85. }else {
  86. x += 60 + margin;
  87. }
  88. // if(fmod(i*tempN+j+1, tempN/2)==0.0f && j >= (tempN/2-1)){
  89. // x = WIDTH_PAGE*i + startX;
  90. // y += 70;
  91. // }else
  92. // x += 60;
  93. n++;
  94. }
  95. }
  96. _pc = [[UIPageControl alloc]initWithFrame:CGRectMake(100, self.frame.size.height-30, JX_SCREEN_WIDTH-200, 30)];
  97. _pc.numberOfPages = maxPage;
  98. _pc.pageIndicatorTintColor = [UIColor grayColor];
  99. _pc.currentPageIndicatorTintColor = [UIColor blackColor];
  100. [_pc addTarget:self action:@selector(actionPage) forControlEvents:UIControlEventTouchUpInside];
  101. [self addSubview:_pc];
  102. // [_pc release];
  103. }
  104. -(void)actionSelect:(UIView*)sender
  105. {
  106. NSString* s = [faceArray objectAtIndex:sender.tag];
  107. NSString *text = [s lastPathComponent];
  108. if ([self.delegate respondsToSelector:@selector(selectGifWithString:)]) {
  109. [self.delegate selectGifWithString:text];
  110. }
  111. // NSString* s = [faceArray objectAtIndex:sender.tag];
  112. // if( [_delegate isKindOfClass:[UITextField class]] ){
  113. // UITextField* p = _delegate;
  114. // int old = p.tag;
  115. // p.tag = kWCMessageTypeGif;
  116. // p.text = [s lastPathComponent];
  117. // if(p.delegate)
  118. // [p.delegate textFieldShouldReturn:p];
  119. // p.tag = old;
  120. // p = nil;
  121. // }
  122. }
  123. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  124. {
  125. int index = scrollView.contentOffset.x/JX_SCREEN_WIDTH;
  126. int mod = fmod(scrollView.contentOffset.x,JX_SCREEN_WIDTH);
  127. if( mod >= JX_SCREEN_WIDTH/2)
  128. index++;
  129. _pc.currentPage = index;
  130. // [self setPage];
  131. }
  132. - (void) setPage
  133. {
  134. _sv.contentOffset = CGPointMake(WIDTH_PAGE*_pc.currentPage, 0.0f);
  135. // NSLog(@"setPage:%d,%f",_sv.contentOffset,_pc.currentPage);
  136. [_pc setNeedsDisplay];
  137. }
  138. -(void)actionPage{
  139. [self setPage];
  140. }
  141. @end