// // DSWebMenuCollectionView.m // VietnamGame // // #import "DSWebMenuCollectionView.h" #import "DSAvatarCollectionViewCell.h" static NSString *identifier = @"AvatarCollectionViewCell"; @implementation DSWebMenuCollectionView -(void)awakeFromNib { [super awakeFromNib]; UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init]; flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical; self.collectionViewLayout = flowlayout; self.delegate = self; self.dataSource = self; self.showsVerticalScrollIndicator = NO; self.showsHorizontalScrollIndicator = NO; self.backgroundColor = [UIColor clearColor]; [self registerNib:[UINib nibWithNibName:@"DSAvatarCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:identifier]; [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:identifier]; } -(void)setShareArray:(NSMutableArray *)shareArray { _shareArray = shareArray; [self reloadData]; } #pragma mark - 推荐 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.shareArray.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(60, 60); } -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 0, 10, 0); } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 0; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 10; } -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:identifier forIndexPath:indexPath]; return view; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { DSAvatarCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; id value = self.shareArray[indexPath.row]; if ([value isKindOfClass:[LinkConfig class]]) { LinkConfig *link = value; if (ISNULLSTR(link.icon)) { cell.avatarImageView.image = [self circularImageWithText:link.name]; }else{ NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:link.icon] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if (error) { NSLog(@"Error downloading image: %@", error); } else { dispatch_async(dispatch_get_main_queue(), ^{ UIImage *image = [UIImage imageWithData:data]; cell.avatarImageView.image = image; }); } }]; [dataTask resume]; } }else if ([value isKindOfClass:[NSNumber class]]) { NSNumber *number = value; DSWebMenuType type = number.integerValue; if (type == DSWebMenuHomeType) { cell.avatarImageView.image = [UIImage imageNamed:@"menu"]; }else if (type == DSWebMenuRefreshType) { cell.avatarImageView.image = [UIImage imageNamed:@"refresh"]; }else if (type == DSWebMenuTelegramType) { cell.avatarImageView.image = [UIImage imageNamed:@"telegram"]; }else if (type == DSWebMenuWhatsAppType) { cell.avatarImageView.image = [UIImage imageNamed:@"whatsapp"]; }else if (type == DSWebMenuFacebookType) { cell.avatarImageView.image = [UIImage imageNamed:@"facebook"]; }else if (type == DSWebMenuNotiType) { cell.avatarImageView.image = [UIImage imageNamed:@"noti"]; } } cell.avatarImageView.contentMode = UIViewContentModeScaleAspectFit; return cell; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { id value = self.shareArray[indexPath.row]; if (self.webMenuDelegate && [self.webMenuDelegate respondsToSelector:@selector(webMenuCollectionViewWithMenuType:)]) { [self.webMenuDelegate webMenuCollectionViewWithMenuType:value]; } } - (UIImage *)circularImageWithText:(NSString *)text { // 设置字体和大小 UIFont *font = [UIFont systemFontOfSize:16.0f]; CGSize textSize = [text sizeWithFont:font]; // 创建图形上下文 CGFloat size = 60.0f; UIGraphicsBeginImageContextWithOptions(CGSizeMake(size, size), NO, 0.0); CGContextRef context = UIGraphicsGetCurrentContext(); // 绘制圆形路径 CGRect rect = CGRectMake(0, 0, size, size); CGContextAddEllipseInRect(context, rect); CGContextClip(context); // 设置背景颜色(可选) CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); // 白色背景 CGContextFillRect(context, rect); // 设置文本颜色 [[UIColor redColor] set]; // 计算文本绘制位置,使其居中 CGFloat x = (size - textSize.width) / 2.0f; CGFloat y = (size - textSize.height) / 2.0f; // 绘制文本 [text drawAtPoint:CGPointMake(x, y) withFont:font]; // 获取UIImage对象 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); // 结束图形上下文 UIGraphicsEndImageContext(); return image; } @end