EmojiPackgeViewController.m 8.6 KB

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