FavoritesVC.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // FavoritesVC.m
  3. // shiku_im
  4. //
  5. // Created by p on 2017/9/14.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "FavoritesVC.h"
  9. @interface FavoritesVC ()<UIScrollViewDelegate>
  10. @property (nonatomic, assign) int margin;
  11. @property (nonatomic, assign) int tempN;
  12. @property (nonatomic, assign) int maxPage;
  13. @property (nonatomic, strong) UIScrollView *sv;
  14. @property (nonatomic, strong) UIPageControl *pc;
  15. @property (nonatomic, strong) NSMutableArray *delBtns;
  16. @end
  17. @implementation FavoritesVC
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. _delBtns = [NSMutableArray array];
  21. _margin = 18;
  22. // tempN = (JX_SCREEN_WIDTH <= 320) ? 8:10;
  23. _tempN = JX_SCREEN_WIDTH / (60 + _margin);
  24. if (((_tempN + 1) * 60 + _tempN * _margin) <= JX_SCREEN_WIDTH) {
  25. _tempN += 1;
  26. }
  27. _margin = (JX_SCREEN_WIDTH - _tempN * 60) / (_tempN + 1);
  28. // if (g_myself.favorites.count <= 0) {
  29. // [g_server userCollectionListWithType:6 pageIndex:0 toView:self];
  30. // [g_server userEmojiListWithPageIndex:0 toView:self];
  31. [g_server faceClollectListType:@"1" View:self];
  32. // }
  33. [g_notify addObserver:self selector:@selector(refresh) name:kFavoritesRefresh object:nil];
  34. }
  35. -(void)create {
  36. int m = fmod([g_myself.favorites count], (_tempN * 2));
  37. _maxPage = (int)[g_myself.favorites count]/(_tempN*2);
  38. if(m != 0)
  39. _maxPage++;
  40. [self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  41. [_delBtns removeAllObjects];
  42. _sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-20)];
  43. _sv.contentSize = CGSizeMake(WIDTH_PAGE*_maxPage, self.view.frame.size.height-20);
  44. _sv.pagingEnabled = YES;
  45. _sv.scrollEnabled = YES;
  46. _sv.delegate = self;
  47. _sv.showsVerticalScrollIndicator = NO;
  48. _sv.showsHorizontalScrollIndicator = NO;
  49. _sv.userInteractionEnabled = YES;
  50. _sv.minimumZoomScale = 1;
  51. _sv.maximumZoomScale = 1;
  52. _sv.decelerationRate = 0.01f;
  53. _sv.backgroundColor = [UIColor clearColor];
  54. [self.view addSubview:_sv];
  55. // [_sv release];
  56. int n = 0;
  57. int startX = (JX_SCREEN_WIDTH - _tempN * 60 - (_tempN - 1) * _margin) / 2;
  58. for(int i=0;i<_maxPage;i++){
  59. int x=WIDTH_PAGE*i + startX,y=0;
  60. for(int j=0;j<_tempN * 2;j++){
  61. if(n>=[g_myself.favorites count])
  62. break;
  63. JXImageView *iv = [[JXImageView alloc] initWithFrame:CGRectMake(x, y+10, 60, 60)];
  64. iv.tag = n;
  65. NSDictionary *dict = g_myself.favorites[n];
  66. NSString *url = dict[@"url"];
  67. [iv sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"Default_Gray"]];
  68. iv.delegate = self;
  69. iv.didTouch = @selector(actionSelect:);
  70. [_sv addSubview:iv];
  71. // 长按删除手势
  72. UILongPressGestureRecognizer *lg = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGestureAction:)];
  73. [iv addGestureRecognizer:lg];
  74. // 删除按钮
  75. JXImageView* del = [[JXImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(iv.frame) - 15, iv.frame.origin.y - 5, 20, 20)];
  76. del.didTouch = @selector(onDelete:);
  77. del.delegate = self;
  78. del.tag = n;
  79. del.image = [UIImage imageNamed:@"delete"];
  80. del.hidden = YES;
  81. [_sv addSubview:del];
  82. [_delBtns addObject:del];
  83. if ((j + 1) % _tempN == 0) {
  84. x = WIDTH_PAGE*i + startX;
  85. y += 70;
  86. }else {
  87. x += 60 + _margin;
  88. }
  89. n++;
  90. }
  91. }
  92. _pc = [[UIPageControl alloc]initWithFrame:CGRectMake(100, self.view.frame.size.height-30, JX_SCREEN_WIDTH-200, 30)];
  93. _pc.numberOfPages = _maxPage;
  94. _pc.pageIndicatorTintColor = [UIColor grayColor];
  95. _pc.currentPageIndicatorTintColor = [UIColor blackColor];
  96. _pc.userInteractionEnabled = NO;
  97. [_pc addTarget:self action:@selector(actionPage) forControlEvents:UIControlEventTouchUpInside];
  98. [self.view addSubview:_pc];
  99. }
  100. - (void)refresh {
  101. [g_server faceClollectListType:@"1" View:self];
  102. [self create];
  103. }
  104. // 点击发送
  105. -(void)actionSelect:(UIView*)sender
  106. {
  107. NSDictionary *dict = [g_myself.favorites objectAtIndex:sender.tag];
  108. NSString* s = dict[@"url"];
  109. if ([self.delegate respondsToSelector:@selector(selectFavoritWithString:)]) {
  110. [self.delegate selectFavoritWithString:s];
  111. }
  112. }
  113. // 长按显示删除按钮
  114. - (void)longGestureAction:(UILongPressGestureRecognizer *)gestureRecognizer {
  115. if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
  116. NSInteger n = gestureRecognizer.view.tag;
  117. for (NSInteger i = 0; i < _delBtns.count; i ++) {
  118. JXImageView *iv = _delBtns[i];
  119. if (i == n) {
  120. iv.hidden = !iv.hidden;
  121. }else {
  122. iv.hidden = YES;
  123. }
  124. }
  125. }
  126. }
  127. // 删除
  128. - (void)onDelete:(UIView *)view {
  129. NSInteger n = view.tag;
  130. // if ([self.delegate respondsToSelector:@selector(deleteFavoritWithString:)]) {
  131. // NSDictionary *dict = [g_myself.favorites objectAtIndex:n];
  132. // NSString* s = dict[@"emojiId"];
  133. // [self.delegate deleteFavoritWithString:s];
  134. // [g_myself.favorites removeObjectAtIndex:n];
  135. // [self create];
  136. // }
  137. NSDictionary *dict = [g_myself.favorites objectAtIndex:n];
  138. NSString* s = dict[@"id"];
  139. [g_server faceClollectDeleteFaceClollect:s View:self];
  140. [g_myself.favorites removeObjectAtIndex:n];
  141. [self create];
  142. }
  143. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  144. for (NSInteger i = 0; i < _delBtns.count; i ++) {
  145. JXImageView *iv = _delBtns[i];
  146. iv.hidden = YES;
  147. }
  148. }
  149. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  150. {
  151. int index = scrollView.contentOffset.x/JX_SCREEN_WIDTH;
  152. int mod = fmod(scrollView.contentOffset.x,JX_SCREEN_WIDTH);
  153. if( mod >= JX_SCREEN_WIDTH/2)
  154. index++;
  155. _pc.currentPage = index;
  156. }
  157. - (void) setPage
  158. {
  159. _sv.contentOffset = CGPointMake(WIDTH_PAGE*_pc.currentPage, 0.0f);
  160. [_pc setNeedsDisplay];
  161. }
  162. -(void)actionPage{
  163. [self setPage];
  164. }
  165. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  166. if ([aDownload.action isEqualToString:act_userEmojiList]) {
  167. [g_myself.favorites removeAllObjects];
  168. [g_myself.favorites addObjectsFromArray:array1];
  169. [self create];
  170. }
  171. if ([aDownload.action isEqualToString:act_FaceClollectList]) {
  172. // NSLog(@"----%@---%@", dict, array1);
  173. [g_myself.favorites removeAllObjects];
  174. [g_myself.favorites addObjectsFromArray:array1];
  175. [self create];
  176. }
  177. if([aDownload.action isEqualToString:act_FaceClollectDeleteFaceClollect]){
  178. // 删除成功
  179. [SVProgressHUD setMinimumDismissTimeInterval:2.0];
  180. [SVProgressHUD showSuccessWithStatus:@"删除成功"];
  181. // 通知键盘
  182. [g_server faceClollectListType:@"1" View:self];
  183. }
  184. }
  185. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  186. return hide_error;
  187. }
  188. -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时
  189. return hide_error;
  190. }
  191. -(void) didServerConnectStart:(JXConnection*)aDownload{
  192. }
  193. - (void)didReceiveMemoryWarning {
  194. [super didReceiveMemoryWarning];
  195. // Dispose of any resources that can be recreated.
  196. }
  197. /*
  198. #pragma mark - Navigation
  199. // In a storyboard-based application, you will often want to do a little preparation before navigation
  200. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  201. // Get the new view controller using [segue destinationViewController].
  202. // Pass the selected object to the new view controller.
  203. }
  204. */
  205. @end