Automatically relayout cells after orientation changed.

This commit is contained in:
Huy Nguyen
2015-08-02 18:01:28 +03:00
parent 8da9ddd884
commit e657bedd40
6 changed files with 122 additions and 0 deletions

View File

@@ -15,6 +15,7 @@
#import "ASDisplayNodeInternal.h"
#import "ASBatchFetching.h"
#import "UICollectionViewLayout+ASConvenience.h"
#import "ASInternalHelpers.h"
const static NSUInteger kASCollectionViewAnimationNone = UITableViewRowAnimationNone;
@@ -121,6 +122,8 @@ static BOOL _isInterceptedSelector(SEL sel)
BOOL _implementsInsetSection;
ASBatchContext *_batchContext;
BOOL _pendingRelayoutForAllRows;
}
@property (atomic, assign) BOOL asyncDataSourceLocked;
@@ -168,6 +171,11 @@ static BOOL _isInterceptedSelector(SEL sel)
[self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"_ASCollectionViewCell"];
if (ASSystemVersionLessThan8()) {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}
return self;
}
@@ -177,6 +185,11 @@ static BOOL _isInterceptedSelector(SEL sel)
// This bug might be iOS 7-specific.
super.delegate = nil;
super.dataSource = nil;
if (ASSystemVersionLessThan8()) {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}
}
#pragma mark -
@@ -462,6 +475,32 @@ static BOOL _isInterceptedSelector(SEL sel)
}
#pragma mark -
#pragma mark Orientation Change Handling
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
_pendingRelayoutForAllRows = YES;
}
- (void)deviceOrientationDidChange
{
_pendingRelayoutForAllRows = YES;
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (_pendingRelayoutForAllRows) {
_pendingRelayoutForAllRows = NO;
[self performBatchAnimated:NO updates:^{
[_dataController relayoutAllRows];
} completion:nil];
}
}
#pragma mark -
#pragma mark Batch Fetching