
主要变更: 1. 新增 EPMomentPublishViewController.h 和 EPMomentPublishViewController.m 文件,提供图文发布页面的 UI 和逻辑。 2. 实现了发布按钮、文本输入框、图片选择功能,支持最多选择 9 张图片。 3. 集成了 TZImagePickerController 以便于用户选择图片。 4. 更新了 EPMomentViewController,添加了跳转到发布页面的逻辑。 此功能旨在提升用户体验,简化图文发布流程。
207 lines
11 KiB
Objective-C
207 lines
11 KiB
Objective-C
//
|
|
// EPMomentPublishViewController.m
|
|
// YuMi
|
|
//
|
|
// Created by AI on 2025-10-10.
|
|
//
|
|
|
|
#import "EPMomentPublishViewController.h"
|
|
#import <Masonry/Masonry.h>
|
|
#import <TZImagePickerController/TZImagePickerController.h>
|
|
#import "DJDKMIMOMColor.h"
|
|
#import "SZTextView.h"
|
|
|
|
@interface EPMomentPublishViewController () <UICollectionViewDataSource, UICollectionViewDelegate, TZImagePickerControllerDelegate, UITextViewDelegate>
|
|
|
|
@property (nonatomic, strong) UIView *navView;
|
|
@property (nonatomic, strong) UIButton *backButton;
|
|
@property (nonatomic, strong) UILabel *titleLabel;
|
|
@property (nonatomic, strong) UIButton *publishButton;
|
|
|
|
@property (nonatomic, strong) UIView *contentView;
|
|
@property (nonatomic, strong) SZTextView *textView;
|
|
@property (nonatomic, strong) UILabel *limitLabel;
|
|
@property (nonatomic, strong) UIView *lineView;
|
|
@property (nonatomic, strong) UICollectionView *collectionView;
|
|
@property (nonatomic, strong) NSMutableArray<UIImage *> *images;
|
|
@property (nonatomic, strong) NSMutableArray *selectedAssets; // TZImagePicker 已选资源
|
|
|
|
@end
|
|
|
|
@implementation EPMomentPublishViewController
|
|
|
|
- (void)viewDidLoad {
|
|
[super viewDidLoad];
|
|
self.view.backgroundColor = [UIColor colorWithRed:0x0C/255.0 green:0x05/255.0 blue:0x27/255.0 alpha:1.0];
|
|
[self setupUI];
|
|
}
|
|
|
|
- (void)setupUI {
|
|
[self.view addSubview:self.navView];
|
|
[self.view addSubview:self.contentView];
|
|
[self.navView addSubview:self.backButton];
|
|
[self.navView addSubview:self.titleLabel];
|
|
// 发布按钮移到底部
|
|
[self.contentView addSubview:self.textView];
|
|
[self.contentView addSubview:self.limitLabel];
|
|
[self.contentView addSubview:self.lineView];
|
|
[self.contentView addSubview:self.collectionView];
|
|
[self.contentView addSubview:self.publishButton];
|
|
|
|
[self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.leading.trailing.top.equalTo(self.view);
|
|
make.height.mas_equalTo(kNavigationHeight);
|
|
}];
|
|
[self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.leading.equalTo(self.view).offset(10);
|
|
make.top.mas_equalTo(statusbarHeight);
|
|
make.size.mas_equalTo(CGSizeMake(44, 44));
|
|
}];
|
|
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.centerX.equalTo(self.navView);
|
|
make.centerY.equalTo(self.backButton);
|
|
}];
|
|
// 发布按钮约束移到底部
|
|
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.leading.trailing.equalTo(self.view);
|
|
make.top.equalTo(self.navView.mas_bottom);
|
|
make.bottom.equalTo(self.view);
|
|
}];
|
|
[self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.leading.trailing.equalTo(self.contentView).inset(15);
|
|
make.top.equalTo(self.contentView).offset(10);
|
|
make.height.mas_equalTo(150);
|
|
}];
|
|
[self.limitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.equalTo(self.textView.mas_bottom).offset(5);
|
|
make.trailing.equalTo(self.textView);
|
|
}];
|
|
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.equalTo(self.limitLabel.mas_bottom).offset(10);
|
|
make.leading.trailing.equalTo(self.textView);
|
|
make.height.mas_equalTo(1);
|
|
}];
|
|
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.leading.trailing.equalTo(self.contentView).inset(15);
|
|
make.top.equalTo(self.lineView.mas_bottom).offset(10);
|
|
make.height.mas_equalTo(110);
|
|
}];
|
|
|
|
// 底部发布按钮
|
|
[self.publishButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.leading.trailing.equalTo(self.view).inset(20);
|
|
make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-20);
|
|
make.height.mas_equalTo(50);
|
|
}];
|
|
}
|
|
|
|
#pragma mark - Actions
|
|
|
|
- (void)onBack {
|
|
[self dismissViewControllerAnimated:YES completion:nil];
|
|
}
|
|
|
|
- (void)onPublish {
|
|
// TODO: 挂接实际发布逻辑
|
|
[self dismissViewControllerAnimated:YES completion:nil];
|
|
}
|
|
|
|
#pragma mark - UICollectionView
|
|
|
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
|
|
return self.images.count + 1; // 最后一个是添加按钮
|
|
}
|
|
|
|
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ep.publish.cell" forIndexPath:indexPath];
|
|
cell.contentView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.06];
|
|
cell.contentView.layer.cornerRadius = 12;
|
|
// 清空复用子视图,避免加号被覆盖
|
|
for (UIView *sub in cell.contentView.subviews) { [sub removeFromSuperview]; }
|
|
BOOL showAdd = (self.images.count < 9) && (indexPath.item == self.images.count);
|
|
if (showAdd) {
|
|
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mine_user_info_album_add"]];
|
|
[cell.contentView addSubview:iv];
|
|
[iv mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(cell.contentView); make.size.mas_equalTo(CGSizeMake(24, 24)); }];
|
|
} else {
|
|
UIImageView *iv = [[UIImageView alloc] init];
|
|
iv.contentMode = UIViewContentModeScaleAspectFill;
|
|
iv.layer.masksToBounds = YES;
|
|
[cell.contentView addSubview:iv];
|
|
[iv mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(cell.contentView); }];
|
|
NSInteger idx = MIN(indexPath.item, (NSInteger)self.images.count - 1);
|
|
if (idx >= 0 && idx < self.images.count) iv.image = self.images[idx];
|
|
}
|
|
return cell;
|
|
}
|
|
|
|
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
if (indexPath.item == self.images.count) {
|
|
TZImagePickerController *picker = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];
|
|
picker.allowPickingVideo = NO;
|
|
picker.allowTakeVideo = NO;
|
|
picker.selectedAssets = self.selectedAssets; // 预选
|
|
picker.maxImagesCount = 9; // 总上限
|
|
[self presentViewController:picker animated:YES completion:nil];
|
|
}
|
|
}
|
|
|
|
#pragma mark - TZImagePickerControllerDelegate
|
|
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos {
|
|
// 合并选择:在已有基础上追加,最多 9 张
|
|
for (NSInteger i = 0; i < assets.count; i++) {
|
|
id asset = assets[i];
|
|
UIImage *img = [photos xpSafeObjectAtIndex:i] ?: photos[i];
|
|
if (![self.selectedAssets containsObject:asset] && self.images.count < 9) {
|
|
[self.selectedAssets addObject:asset];
|
|
[self.images addObject:img];
|
|
}
|
|
}
|
|
[self.collectionView reloadData];
|
|
}
|
|
|
|
#pragma mark - UITextViewDelegate
|
|
- (void)textViewDidChange:(UITextView *)textView {
|
|
if (textView.text.length > 500) {
|
|
textView.text = [textView.text substringToIndex:500];
|
|
}
|
|
self.limitLabel.text = [NSString stringWithFormat:@"%lu/500", (unsigned long)textView.text.length];
|
|
}
|
|
|
|
#pragma mark - Lazy
|
|
|
|
- (UIView *)navView { if (!_navView) { _navView = [UIView new]; _navView.backgroundColor = [UIColor clearColor]; } return _navView; }
|
|
- (UIButton *)backButton { if (!_backButton) { _backButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_backButton setImage:[UIImage imageNamed:@"common_nav_back"] forState:UIControlStateNormal]; [_backButton addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside]; } return _backButton; }
|
|
- (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [UILabel new]; _titleLabel.text = @"图文发布"; _titleLabel.textColor = [DJDKMIMOMColor mainTextColor]; _titleLabel.font = [UIFont systemFontOfSize:17]; } return _titleLabel; }
|
|
- (UIButton *)publishButton {
|
|
if (!_publishButton) {
|
|
_publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
[_publishButton setTitle:@"发布" forState:UIControlStateNormal];
|
|
[_publishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
|
_publishButton.titleLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightMedium];
|
|
_publishButton.layer.cornerRadius = 25;
|
|
_publishButton.layer.masksToBounds = YES;
|
|
// 渐变背景:从浅紫到深紫
|
|
CAGradientLayer *gradient = [CAGradientLayer layer];
|
|
gradient.colors = @[(__bridge id)[UIColor colorWithRed:0.6 green:0.3 blue:0.8 alpha:1.0].CGColor,
|
|
(__bridge id)[UIColor colorWithRed:0.3 green:0.1 blue:0.5 alpha:1.0].CGColor];
|
|
gradient.startPoint = CGPointMake(0, 0);
|
|
gradient.endPoint = CGPointMake(1, 0);
|
|
gradient.frame = CGRectMake(0, 0, 1, 1);
|
|
[_publishButton.layer insertSublayer:gradient atIndex:0];
|
|
[_publishButton addTarget:self action:@selector(onPublish) forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
return _publishButton;
|
|
}
|
|
- (UIView *)contentView { if (!_contentView) { _contentView = [UIView new]; _contentView.backgroundColor = [UIColor clearColor]; } return _contentView; }
|
|
- (SZTextView *)textView { if (!_textView) { _textView = [SZTextView new]; _textView.placeholder = @"Enter Content"; _textView.textColor = [DJDKMIMOMColor mainTextColor]; _textView.placeholderTextColor = [DJDKMIMOMColor secondTextColor]; _textView.font = [UIFont systemFontOfSize:15]; _textView.delegate = self; } return _textView; }
|
|
- (UILabel *)limitLabel { if (!_limitLabel) { _limitLabel = [UILabel new]; _limitLabel.text = @"0/500"; _limitLabel.textColor = [DJDKMIMOMColor mainTextColor]; _limitLabel.font = [UIFont systemFontOfSize:12]; } return _limitLabel; }
|
|
- (UIView *)lineView { if (!_lineView) { _lineView = [UIView new]; _lineView.backgroundColor = [DJDKMIMOMColor dividerColor]; } return _lineView; }
|
|
- (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.minimumLineSpacing = 10; layout.minimumInteritemSpacing = 10; CGFloat itemW = (KScreenWidth - 15*2 - 10*2)/3.0; layout.itemSize = CGSizeMake(itemW, itemW); _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.backgroundColor = [UIColor clearColor]; [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"ep.publish.cell"]; } return _collectionView; }
|
|
- (NSMutableArray<UIImage *> *)images { if (!_images) { _images = [NSMutableArray array]; } return _images; }
|
|
- (NSMutableArray *)selectedAssets { if (!_selectedAssets) { _selectedAssets = [NSMutableArray array]; } return _selectedAssets; }
|
|
|
|
@end
|
|
|
|
|