mirror of
https://github.com/arichornlover/uYouEnhanced.git
synced 2026-03-11 21:26:04 +00:00
Create YTPivotBarReorder.m
This commit is contained in:
parent
72b4ee934f
commit
e80b9177cf
1 changed files with 73 additions and 0 deletions
73
Sources/YTPivotBarReorder.m
Normal file
73
Sources/YTPivotBarReorder.m
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#import "YTPivotBarReorder.h"
|
||||
#import "uYouPlus.h"
|
||||
|
||||
@interface YTPivotBarReorder ()
|
||||
|
||||
@property (strong, nonatomic) UICollectionView *collectionView;
|
||||
@property (strong, nonatomic) NSMutableArray *pivotBarItems;
|
||||
|
||||
@end
|
||||
|
||||
@implementation YTPivotBarReorder
|
||||
|
||||
- (void)viewDidLoad {
|
||||
[super viewDidLoad];
|
||||
self.title = @"Reorder Pivot Bar Icons";
|
||||
self.view.backgroundColor = [UIColor systemBackgroundColor];
|
||||
|
||||
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
|
||||
layout.itemSize = CGSizeMake(100, 100);
|
||||
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
|
||||
|
||||
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
|
||||
self.collectionView.delegate = self;
|
||||
self.collectionView.dataSource = self;
|
||||
self.collectionView.dragDelegate = self;
|
||||
self.collectionView.dropDelegate = self;
|
||||
self.collectionView.dragInteractionEnabled = YES;
|
||||
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
|
||||
[self.view addSubview:self.collectionView];
|
||||
|
||||
self.pivotBarItems = [@[@"Home", @"Shorts", @"Subscriptions", @"Notifications", @"Library"] mutableCopy];
|
||||
}
|
||||
|
||||
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
|
||||
return self.pivotBarItems.count;
|
||||
}
|
||||
|
||||
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
|
||||
UILabel *label = [[UILabel alloc] initWithFrame:cell.contentView.bounds];
|
||||
label.text = self.pivotBarItems[indexPath.row];
|
||||
label.textAlignment = NSTextAlignmentCenter;
|
||||
[cell.contentView addSubview:label];
|
||||
return cell;
|
||||
}
|
||||
|
||||
- (NSArray<UIDragItem *> *)collectionView:(UICollectionView *)collectionView itemsForBeginningDragSession:(id<UIDragSession>)session atIndexPath:(NSIndexPath *)indexPath {
|
||||
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithObject:self.pivotBarItems[indexPath.row]];
|
||||
UIDragItem *dragItem = [[UIDragItem alloc] initWithItemProvider:itemProvider];
|
||||
dragItem.localObject = self.pivotBarItems[indexPath.row];
|
||||
return @[dragItem];
|
||||
}
|
||||
|
||||
- (void)collectionView:(UICollectionView *)collectionView performDropWithCoordinator:(id<UICollectionViewDropCoordinator>)coordinator {
|
||||
NSIndexPath *destinationIndexPath = coordinator.destinationIndexPath;
|
||||
if (!destinationIndexPath) {
|
||||
destinationIndexPath = [NSIndexPath indexPathForRow:self.pivotBarItems.count - 1 inSection:0];
|
||||
}
|
||||
|
||||
for (id<UICollectionViewDropItem> dropItem in coordinator.items) {
|
||||
NSIndexPath *sourceIndexPath = dropItem.sourceIndexPath;
|
||||
if (sourceIndexPath) {
|
||||
[collectionView performBatchUpdates:^{
|
||||
id item = self.pivotBarItems[sourceIndexPath.row];
|
||||
[self.pivotBarItems removeObjectAtIndex:sourceIndexPath.row];
|
||||
[self.pivotBarItems insertObject:item atIndex:destinationIndexPath.row];
|
||||
[collectionView moveItemAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
|
||||
} completion:nil];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
Loading…
Reference in a new issue