Skip to content

Commit 8754714

Browse files
author
Bluelich
committed
update demo
1 parent f1807f1 commit 8754714

File tree

4 files changed

+79
-96
lines changed

4 files changed

+79
-96
lines changed

BLCollectionViewTagLayout/Classes/BLCollectionViewTagLayout.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ typedef NS_ENUM(NSUInteger, BLCollectionViewTagLayoutAnimationState) {
2727
/**
2828
* Animation configuration for elemets appearing or disappearing
2929
*
30-
* @param indexPath NSIndexPath
31-
* @param kind kind of the element, or nil if the catrgory is cell
32-
* @param catrgory UICollectionElementCategory
30+
* @param originalLayoutAttributes original UICollectionViewLayoutAttributes
3331
*/
3432
typedef
3533
UICollectionViewLayoutAttributes * _Nullable(^BLCollectionViewTagLayoutAnimation)
36-
(NSIndexPath *indexPath,NSString * _Nullable kind,UICollectionElementCategory catrgory);
34+
(UICollectionViewLayoutAttributes * _Nullable originalLayoutAttributes);
3735

3836
#pragma mark - UICollectionViewTagStyleLayout
3937
#pragma mark -

BLCollectionViewTagLayout/Classes/BLCollectionViewTagLayout.m

+16-18
Original file line numberDiff line numberDiff line change
@@ -832,54 +832,52 @@ - (void)finalizeLayoutTransition
832832
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
833833
{
834834
if (self.elementAppearingAnimationFromValue) {
835-
self.elementAppearingAnimationFromValue(itemIndexPath,
836-
nil,
837-
UICollectionElementCategoryCell);
835+
return self.elementAppearingAnimationFromValue(
836+
[self layoutAttributesForItemAtIndexPath:itemIndexPath].copy);
838837
}
839838
return [super initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];
840839
}
841840
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
842841
{
843842
if (self.elementDisappearingAnimationToValue) {
844-
self.elementDisappearingAnimationToValue(itemIndexPath,
845-
nil,
846-
UICollectionElementCategoryCell);
843+
return self.elementDisappearingAnimationToValue(
844+
[self layoutAttributesForItemAtIndexPath:itemIndexPath].copy);
847845
}
848846
return [super finalLayoutAttributesForDisappearingItemAtIndexPath:itemIndexPath];
849847
}
850848
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath
851849
{
852850
if (self.elementAppearingAnimationFromValue) {
853-
self.elementAppearingAnimationFromValue(elementIndexPath,
854-
elementKind,
855-
UICollectionElementCategorySupplementaryView);
851+
return self.elementAppearingAnimationFromValue(
852+
[self layoutAttributesForSupplementaryViewOfKind:elementKind
853+
atIndexPath:elementIndexPath].copy);
856854
}
857855
return [super initialLayoutAttributesForAppearingSupplementaryElementOfKind:elementKind atIndexPath:elementIndexPath];
858856
}
859857
- (nullable UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath
860858
{
861859
if (self.elementDisappearingAnimationToValue) {
862-
self.elementDisappearingAnimationToValue(elementIndexPath,
863-
elementKind,
864-
UICollectionElementCategorySupplementaryView);
860+
return self.elementDisappearingAnimationToValue(
861+
[self layoutAttributesForSupplementaryViewOfKind:elementKind
862+
atIndexPath:elementIndexPath].copy);
865863
}
866864
return [super finalLayoutAttributesForDisappearingSupplementaryElementOfKind:elementKind atIndexPath:elementIndexPath];
867865
}
868866
- (nullable UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingDecorationElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)decorationIndexPath
869867
{
870868
if (self.elementAppearingAnimationFromValue) {
871-
self.elementAppearingAnimationFromValue(decorationIndexPath,
872-
elementKind,
873-
UICollectionElementCategoryDecorationView);
869+
return self.elementAppearingAnimationFromValue(
870+
[self layoutAttributesForDecorationViewOfKind:elementKind
871+
atIndexPath:decorationIndexPath].copy);
874872
}
875873
return [super initialLayoutAttributesForAppearingDecorationElementOfKind:elementKind atIndexPath:decorationIndexPath];
876874
}
877875
- (nullable UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingDecorationElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)decorationIndexPath
878876
{
879877
if (self.elementDisappearingAnimationToValue) {
880-
self.elementDisappearingAnimationToValue(decorationIndexPath,
881-
elementKind,
882-
UICollectionElementCategoryDecorationView);
878+
return self.elementDisappearingAnimationToValue(
879+
[self layoutAttributesForDecorationViewOfKind:elementKind
880+
atIndexPath:decorationIndexPath].copy);
883881
}
884882
return [super finalLayoutAttributesForDisappearingDecorationElementOfKind:elementKind atIndexPath:decorationIndexPath];
885883
}

Example/BLCollectionViewTagLayout/BLViewController.m

+47-50
Original file line numberDiff line numberDiff line change
@@ -120,23 +120,17 @@ - (void)viewDidLoad {
120120
[super viewDidLoad];
121121
self.clearsSelectionOnViewWillAppear = NO;
122122

123+
[self updateCollectionViewLayoutConfiguration];
124+
123125
self.collectionView.contentInset = UIEdgeInsetsMake(44, 44, 44, 44);
124126
// self.collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(14, 0, 14, 0);
125-
[self.blCollectionViewLayout registerClass:BLDecorationView.class forDecorationViewOfKind:BLCollectionElementKindSectionDecoration];
126-
[self.blCollectionViewLayout
127-
autoConfigSystemAdditionalAdjustedContentInsetWith:UIApplication.sharedApplication.statusBarFrame
128-
navigationBar:self.navigationController.navigationBar
129-
tabbar:self.tabBarController.tabBar];
130-
131127
[self.collectionView registerClass:UICollectionReusableView.class
132128
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:UICollectionElementKindSectionHeader];
133129
[self.collectionView registerClass:UICollectionReusableView.class
134130
forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:UICollectionElementKindSectionFooter];
135131
NSMutableArray *array = @[].mutableCopy;
136132
for (unichar var = 'A'; var <= 'Z'; var++) {
137-
NSString *text =
138-
[NSString stringWithCharacters:&var length:1];
139-
[array addObject:text];
133+
[array addObject:[NSString stringWithCharacters:&var length:1]];
140134
}
141135
self.alphabetArray = array.copy;
142136
self.dataSource = @[self.alphabetArray.mutableCopy].mutableCopy;
@@ -145,45 +139,44 @@ - (void)viewDidLoad {
145139
[self add:nil];
146140
[self add:nil];
147141
[self add:nil];
142+
if (!self.navigationController) {
143+
self.navigationBar.hidden = NO;
144+
}
148145
}
149-
- (void)viewWillAppear:(BOOL)animated
150-
{
151-
[super viewWillAppear:animated];
152-
if (self.navigationController) return;
153-
self.navigationBar.hidden = NO;
154-
}
155-
- (void)viewWillDisappear:(BOOL)animated
146+
- (void)updateCollectionViewLayoutConfiguration
156147
{
157-
[super viewWillDisappear:animated];
158-
if (self.navigationController) return;
159-
self.navigationBar.hidden = YES;
148+
self.blCollectionViewLayout.elementAppearingAnimationFromValue =
149+
^UICollectionViewLayoutAttributes * _Nullable(UICollectionViewLayoutAttributes * _Nullable originalLayoutAttributes) {
150+
originalLayoutAttributes.alpha = 0.1;
151+
return originalLayoutAttributes;
152+
};
153+
self.blCollectionViewLayout.elementDisappearingAnimationToValue =
154+
^UICollectionViewLayoutAttributes * _Nullable(UICollectionViewLayoutAttributes * _Nullable originalLayoutAttributes) {
155+
originalLayoutAttributes.alpha = 0.1;
156+
return originalLayoutAttributes;
157+
};
158+
[self.blCollectionViewLayout registerClass:BLDecorationView.class forDecorationViewOfKind:BLCollectionElementKindSectionDecoration];
159+
[self.blCollectionViewLayout
160+
autoConfigSystemAdditionalAdjustedContentInsetWith:UIApplication.sharedApplication.statusBarFrame
161+
navigationBar:self.navigationController.navigationBar
162+
tabbar:self.tabBarController.tabBar];
160163
}
161-
162164
- (UINavigationBar *)navigationBar
163165
{
164-
if (self.navigationController) return nil;
165166
if (!_navigationBar) {
166167
UINavigationItem *item = UINavigationItem.new;
167-
item.title = @"Fake NavigationBar";
168168
item.leftBarButtonItems =
169169
@[
170170
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add:)],
171-
[[UIBarButtonItem alloc] initWithTitle:@" - " style:UIBarButtonItemStyleDone target:self action:@selector(delete:)],
171+
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(add:)],
172172
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)]
173173
];
174174
item.rightBarButtonItems =
175175
@[
176-
[[UIBarButtonItem alloc] initWithTitle:@"Decoration" style:UIBarButtonItemStyleDone target:self action:@selector(showDecoration:)],
177-
[[UIBarButtonItem alloc] initWithTitle:@"UnPin" style:UIBarButtonItemStyleDone target:self action:@selector(unPin:)],
178-
[[UIBarButtonItem alloc] initWithTitle:@"Pin" style:UIBarButtonItemStyleDone target:self action:@selector(pin:)]
176+
[[UIBarButtonItem alloc] initWithTitle:@"Pin" style:UIBarButtonItemStylePlain target:self action:@selector(pin:)],
177+
[[UIBarButtonItem alloc] initWithTitle:@"Decoration" style:UIBarButtonItemStylePlain target:self action:@selector(decoration:)]
179178
];
180-
[item.leftBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
181-
obj.style = UIBarButtonItemStyleDone;
182-
}];
183-
[item.rightBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
184-
obj.style = UIBarButtonItemStyleDone;
185-
}];
186-
item.prompt = @"Prompt";
179+
item.prompt = @"Fake NavigationBar";
187180
_navigationBar = UINavigationBar.new;
188181
_navigationBar.translucent = YES;
189182
_navigationBar.backgroundColor = UIColor.clearColor;
@@ -226,15 +219,17 @@ - (BLCollectionViewTagLayout *)blCollectionViewLayout
226219
}
227220
- (IBAction)pin:(UIBarButtonItem *)sender
228221
{
229-
self.blCollectionViewLayout.sectionHeadersPinToVisibleBounds = YES;
230-
self.blCollectionViewLayout.sectionFootersPinToVisibleBounds = YES;
231-
}
232-
- (IBAction)unPin:(UIBarButtonItem *)sender
233-
{
234-
self.blCollectionViewLayout.sectionHeadersPinToVisibleBounds = NO;
235-
self.blCollectionViewLayout.sectionFootersPinToVisibleBounds = NO;
222+
self.blCollectionViewLayout.sectionHeadersPinToVisibleBounds =
223+
!self.blCollectionViewLayout.sectionHeadersPinToVisibleBounds;
224+
self.blCollectionViewLayout.sectionFootersPinToVisibleBounds =
225+
!self.blCollectionViewLayout.sectionFootersPinToVisibleBounds;
226+
if (self.blCollectionViewLayout.sectionHeadersPinToVisibleBounds) {
227+
sender.title = @"Pined";
228+
}else{
229+
sender.title = @"UnPined";
230+
}
236231
}
237-
- (IBAction)showDecoration:(UIBarButtonItem *)sender
232+
- (IBAction)decoration:(UIBarButtonItem *)sender
238233
{
239234
self.blCollectionViewLayout.sectionDecorationVisiable =
240235
!self.blCollectionViewLayout.sectionDecorationVisiable;
@@ -310,15 +305,17 @@ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
310305
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
311306
{
312307
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
313-
[collectionView performBatchUpdates:^{
314-
if (self.dataSource[indexPath.section].count == 1) {
315-
[self.dataSource removeObjectAtIndex:indexPath.section];
316-
[collectionView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
317-
}else{
318-
[self.dataSource[indexPath.section] removeObjectAtIndex:indexPath.item];
319-
[collectionView deleteItemsAtIndexPaths:@[indexPath]];
320-
}
321-
} completion:nil];
308+
[UIView animateWithDuration:2 animations:^{
309+
[collectionView performBatchUpdates:^{
310+
if (self.dataSource[indexPath.section].count == 1) {
311+
[self.dataSource removeObjectAtIndex:indexPath.section];
312+
[collectionView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
313+
}else{
314+
[self.dataSource[indexPath.section] removeObjectAtIndex:indexPath.item];
315+
[collectionView deleteItemsAtIndexPaths:@[indexPath]];
316+
}
317+
} completion:nil];
318+
}];
322319
}
323320
#pragma mark -
324321
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(BLCollectionViewTagLayout *)collectionViewLayout maximumHeightForSection:(NSInteger)section

Example/BLCollectionViewTagLayout/Base.lproj/Main.storyboard

+14-24
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
88
</dependencies>
99
<scenes>
10-
<!--Title-->
10+
<!--View Controller-->
1111
<scene sceneID="cog-Wt-WEy">
1212
<objects>
1313
<collectionViewController id="Svu-6j-u4A" customClass="BLViewController" sceneMemberID="viewController">
@@ -83,39 +83,34 @@
8383
<outlet property="delegate" destination="Svu-6j-u4A" id="l7N-vb-PuP"/>
8484
</connections>
8585
</collectionView>
86-
<navigationItem key="navigationItem" title="Title" prompt="Prompt" id="gJ6-Qm-6rx">
86+
<navigationItem key="navigationItem" prompt="Title and Prompt" id="gJ6-Qm-6rx">
8787
<barButtonItem key="backBarButtonItem" title="Back" id="uAH-CT-Med"/>
8888
<leftBarButtonItems>
89-
<barButtonItem style="done" systemItem="add" id="Rxw-Qe-0Tc">
89+
<barButtonItem style="plain" systemItem="add" id="Rxw-Qe-0Tc">
9090
<connections>
9191
<action selector="add:" destination="Svu-6j-u4A" id="Rdb-f6-9k9"/>
9292
</connections>
9393
</barButtonItem>
94-
<barButtonItem title=" - " style="done" id="M9O-jn-dIT">
94+
<barButtonItem style="plain" systemItem="trash" id="M9O-jn-dIT">
9595
<connections>
9696
<action selector="delete:" destination="Svu-6j-u4A" id="3pc-SL-bdM"/>
9797
</connections>
9898
</barButtonItem>
99-
<barButtonItem style="done" systemItem="refresh" id="Kw8-aZ-3lW">
99+
<barButtonItem style="plain" systemItem="refresh" id="Kw8-aZ-3lW">
100100
<connections>
101101
<action selector="refresh:" destination="Svu-6j-u4A" id="vha-V9-6Jz"/>
102102
</connections>
103103
</barButtonItem>
104104
</leftBarButtonItems>
105105
<rightBarButtonItems>
106-
<barButtonItem title="UnPin" style="done" id="xmZ-cR-fJs">
107-
<connections>
108-
<action selector="unPin:" destination="Svu-6j-u4A" id="sA6-yj-Bpf"/>
109-
</connections>
110-
</barButtonItem>
111-
<barButtonItem title="Pin" style="done" id="AXX-PZ-KD6">
106+
<barButtonItem title="Pin" style="plain" id="AXX-PZ-KD6">
112107
<connections>
113108
<action selector="pin:" destination="Svu-6j-u4A" id="zOz-KK-3fB"/>
114109
</connections>
115110
</barButtonItem>
116-
<barButtonItem title="Decoration" style="done" id="dPe-9n-0RB">
111+
<barButtonItem title="Decoration" style="plain" id="dPe-9n-0RB">
117112
<connections>
118-
<action selector="showDecoration:" destination="Svu-6j-u4A" id="umc-i9-U4N"/>
113+
<action selector="decoration:" destination="Svu-6j-u4A" id="Fw4-xw-Y6G"/>
119114
</connections>
120115
</barButtonItem>
121116
</rightBarButtonItems>
@@ -239,36 +234,31 @@
239234
<navigationItem key="navigationItem" title="Title" id="AvN-EJ-Pj9">
240235
<barButtonItem key="backBarButtonItem" title="Back" id="2fe-FI-J2B"/>
241236
<leftBarButtonItems>
242-
<barButtonItem style="done" systemItem="add" id="Vq9-AN-Qof">
237+
<barButtonItem style="plain" systemItem="add" id="Vq9-AN-Qof">
243238
<connections>
244239
<action selector="add:" destination="AHd-7e-9t4" id="Ujc-sB-wDZ"/>
245240
</connections>
246241
</barButtonItem>
247-
<barButtonItem title=" - " style="done" id="KQO-yE-KK0">
242+
<barButtonItem style="plain" systemItem="trash" id="KQO-yE-KK0">
248243
<connections>
249244
<action selector="delete:" destination="AHd-7e-9t4" id="0z0-ZU-3IU"/>
250245
</connections>
251246
</barButtonItem>
252-
<barButtonItem style="done" systemItem="refresh" id="WuC-rt-yay">
247+
<barButtonItem style="plain" systemItem="refresh" id="WuC-rt-yay">
253248
<connections>
254249
<action selector="refresh:" destination="AHd-7e-9t4" id="Izk-d4-A0g"/>
255250
</connections>
256251
</barButtonItem>
257252
</leftBarButtonItems>
258253
<rightBarButtonItems>
259-
<barButtonItem title="UnPin" style="done" id="nTY-MX-fgg">
260-
<connections>
261-
<action selector="unPin:" destination="AHd-7e-9t4" id="YWg-ls-Dsr"/>
262-
</connections>
263-
</barButtonItem>
264-
<barButtonItem title="Pin" style="done" id="iX7-J4-kut">
254+
<barButtonItem title="Pin" style="plain" id="iX7-J4-kut">
265255
<connections>
266256
<action selector="pin:" destination="AHd-7e-9t4" id="3ty-9H-jbs"/>
267257
</connections>
268258
</barButtonItem>
269-
<barButtonItem title="Decoration" style="done" id="9x9-LH-dF3">
259+
<barButtonItem title="Decoration" style="plain" id="9x9-LH-dF3">
270260
<connections>
271-
<action selector="showDecoration:" destination="Svu-6j-u4A" id="nlN-nD-f3B"/>
261+
<action selector="decoration:" destination="AHd-7e-9t4" id="4x5-vu-LYB"/>
272262
</connections>
273263
</barButtonItem>
274264
</rightBarButtonItems>

0 commit comments

Comments
 (0)