This commit is contained in:
2025-11-08 21:44:41 +08:00
parent a729396401
commit 41b14ceea4
6 changed files with 498 additions and 1 deletions

View File

@@ -0,0 +1,256 @@
//
// MySkinVC.m
// keyBoard
//
//
//
// - Editor/Cancel
// - CollectionView Masonry +
// - cell
// - bottomView >0 Delete #02BEAC
//
#import "MySkinVC.h"
#import <Masonry/Masonry.h>
#import "UIColor+Extension.h"
#import "MySkinCell.h"
static NSString * const kMySkinCellId = @"kMySkinCellId";
@interface MySkinVC () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView; //
@property (nonatomic, strong) UIView *bottomView; //
@property (nonatomic, strong) UILabel *selectedLabel; //
@property (nonatomic, strong) UIButton *deleteButton; //
@property (nonatomic, strong) NSMutableArray<NSDictionary *> *data; //
@property (nonatomic, assign, getter=isEditingMode) BOOL editingMode; //
@end
@implementation MySkinVC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"My Skin"; //
// Editor/Cancel
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Editor" style:UIBarButtonItemStylePlain target:self action:@selector(onToggleEdit)];
//
self.data = [@[
@{ @"title": @"Dopamine" },
@{ @"title": @"Dopamine" },
@{ @"title": @"Dopamine" },
@{ @"title": @"Dopamine" },
] mutableCopy];
//
[self.view addSubview:self.collectionView];
[self.view addSubview:self.bottomView];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
make.left.right.equalTo(self.view);
make.bottom.equalTo(self.view.mas_bottom); // bottomView
}];
[self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
make.height.mas_equalTo(64);
}];
//
self.bottomView.hidden = YES;
}
#pragma mark - Actions
- (void)onToggleEdit {
self.editingMode = !self.editingMode;
//
self.navigationItem.rightBarButtonItem.title = self.isEditingMode ? @"Cancel" : @"Editor";
//
self.bottomView.hidden = !self.isEditingMode;
// /退
self.collectionView.allowsMultipleSelection = self.isEditingMode;
// cell/ markView
for (UICollectionViewCell *c in self.collectionView.visibleCells) {
if (![c isKindOfClass:MySkinCell.class]) continue;
MySkinCell *cell = (MySkinCell *)c;
cell.editing = self.isEditingMode;
NSIndexPath *ip = [self.collectionView indexPathForCell:cell];
BOOL selected = [[self.collectionView indexPathsForSelectedItems] containsObject:ip];
[cell updateSelected:selected];
}
// reload offscreen cell
[self.collectionView reloadData];
//
for (NSIndexPath *ip in [self.collectionView indexPathsForSelectedItems]) {
[self.collectionView deselectItemAtIndexPath:ip animated:NO];
}
[self updateBottomUI];
}
- (void)onDelete {
//
NSArray<NSIndexPath *> *selected = [[self.collectionView indexPathsForSelectedItems] sortedArrayUsingSelector:@selector(compare:)];
if (selected.count == 0) return;
// UI
[self.collectionView performBatchUpdates:^{
NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
for (NSIndexPath *ip in selected) { [set addIndex:ip.item]; }
[self.data removeObjectsAtIndexes:set];
[self.collectionView deleteItemsAtIndexPaths:selected];
} completion:^(BOOL finished) {
[self updateBottomUI];
}];
}
- (void)updateBottomUI {
NSInteger count = self.collectionView.indexPathsForSelectedItems.count;
self.selectedLabel.text = [NSString stringWithFormat:@"Selected: %ld Skins", (long)count];
BOOL enable = count > 0;
self.deleteButton.enabled = enable;
if (enable) {
self.deleteButton.backgroundColor = [UIColor colorWithHex:0x02BEAC];
[self.deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.deleteButton.layer.borderColor = [UIColor colorWithHex:0x02BEAC].CGColor;
} else {
self.deleteButton.backgroundColor = [UIColor colorWithHex:0xF2F2F2];
[self.deleteButton setTitleColor:[UIColor colorWithHex:0xC8C8C8] forState:UIControlStateNormal];
self.deleteButton.layer.borderColor = [UIColor colorWithHex:0xE6E6E6].CGColor;
}
}
#pragma mark - UICollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.data.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MySkinCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kMySkinCellId forIndexPath:indexPath];
NSDictionary *d = self.data[indexPath.item];
[cell configWithTitle:d[@"title"] image:nil];
cell.editing = self.isEditingMode; //
//
BOOL selected = [[collectionView indexPathsForSelectedItems] containsObject:indexPath];
[cell updateSelected:selected];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (!self.isEditingMode) {
//
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
return;
}
MySkinCell *cell = (MySkinCell *)[collectionView cellForItemAtIndexPath:indexPath];
[cell updateSelected:YES];
[self updateBottomUI];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
if (!self.isEditingMode) return;
MySkinCell *cell = (MySkinCell *)[collectionView cellForItemAtIndexPath:indexPath];
[cell updateSelected:NO];
[self updateBottomUI];
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
if (![cell isKindOfClass:MySkinCell.class]) return;
MySkinCell *c = (MySkinCell *)cell;
c.editing = self.isEditingMode; // cell
BOOL selected = [[collectionView indexPathsForSelectedItems] containsObject:indexPath];
[c updateSelected:selected];
}
#pragma mark - Lazy
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
CGFloat inset = 16;
CGFloat spacing = 12;
CGFloat W = UIScreen.mainScreen.bounds.size.width;
CGFloat itemW = floor((W - inset * 2 - spacing) / 2.0);
CGFloat itemH = itemW * 0.82f; //
layout.itemSize = CGSizeMake(itemW, itemH);
layout.minimumInteritemSpacing = spacing;
layout.minimumLineSpacing = spacing;
layout.sectionInset = UIEdgeInsetsMake(12, inset, 12, inset);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.allowsMultipleSelection = NO; //
[_collectionView registerClass:MySkinCell.class forCellWithReuseIdentifier:kMySkinCellId];
}
return _collectionView;
}
- (UIView *)bottomView {
if (!_bottomView) {
_bottomView = [UIView new];
_bottomView.backgroundColor = [UIColor whiteColor];
_bottomView.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.06].CGColor;
_bottomView.layer.shadowOpacity = 1;
_bottomView.layer.shadowOffset = CGSizeMake(0, -2);
[_bottomView addSubview:self.selectedLabel];
[_bottomView addSubview:self.deleteButton];
[self.selectedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_bottomView).offset(16);
make.centerY.equalTo(_bottomView);
}];
[self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(_bottomView).offset(-16);
make.centerY.equalTo(_bottomView);
make.width.mas_equalTo(92);
make.height.mas_equalTo(36);
}];
}
return _bottomView;
}
- (UILabel *)selectedLabel {
if (!_selectedLabel) {
_selectedLabel = [UILabel new];
_selectedLabel.textColor = [UIColor colorWithHex:0x666666];
_selectedLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
_selectedLabel.text = @"Selected: 0 Skins";
}
return _selectedLabel;
}
- (UIButton *)deleteButton {
if (!_deleteButton) {
_deleteButton = [UIButton buttonWithType:UIButtonTypeSystem];
[_deleteButton setTitle:@"Delete" forState:UIControlStateNormal];
_deleteButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
_deleteButton.layer.cornerRadius = 18;
_deleteButton.layer.borderWidth = 1;
_deleteButton.clipsToBounds = YES;
[_deleteButton addTarget:self action:@selector(onDelete) forControlEvents:UIControlEventTouchUpInside];
//
_deleteButton.enabled = NO;
_deleteButton.backgroundColor = [UIColor colorWithHex:0xF2F2F2];
[_deleteButton setTitleColor:[UIColor colorWithHex:0xC8C8C8] forState:UIControlStateNormal];
_deleteButton.layer.borderColor = [UIColor colorWithHex:0xE6E6E6].CGColor;
}
return _deleteButton;
}
@end