iOS: Slider support fit size and slidePosition

This commit is contained in:
pengfei.zhou 2023-04-04 19:50:44 +08:00 committed by osborn
parent 930593fe0c
commit 18d5050c7a
2 changed files with 52 additions and 3 deletions

View File

@ -25,6 +25,13 @@ @interface DoricNestedSliderView : UIScrollView
@end
@implementation DoricNestedSliderView
- (CGSize)sizeThatFits:(CGSize)size {
CGSize result = [super sizeThatFits:size];
if (self.doricLayout.heightSpec == DoricLayoutFit && self.contentSize.height > 0) {
return CGSizeMake(result.width, self.contentSize.height);
}
return result;
}
@end
@interface DoricNestedSliderNode () <UIScrollViewDelegate>

View File

@ -36,6 +36,7 @@ @interface DoricSliderNode () <UICollectionViewDataSource, UICollectionViewDeleg
@property(nonatomic, assign) NSUInteger itemCount;
@property(nonatomic, assign) NSUInteger propItemCount;
@property(nonatomic, assign) NSUInteger batchCount;
@property(nonatomic, assign) NSUInteger slidePosition;
@property(nonatomic, copy) NSString *onPageSelectedFuncId;
@property(nonatomic, assign) BOOL loop;
@property(nonatomic, assign) BOOL propLoop;
@ -46,12 +47,20 @@ @interface DoricSliderNode () <UICollectionViewDataSource, UICollectionViewDeleg
@property(nonatomic, strong) NSString *slideStyle;
@property(nonatomic, assign) CGFloat minScale;
@property(nonatomic, assign) CGFloat maxScale;
@property(nonatomic, assign) BOOL scheduledLayout;
@end
@interface DoricSliderView : UICollectionView
@end
@implementation DoricSliderView
- (CGSize)sizeThatFits:(CGSize)size {
CGSize result = [super sizeThatFits:size];
if (self.doricLayout.heightSpec == DoricLayoutFit && self.contentSize.height > 0) {
return CGSizeMake(result.width, self.contentSize.height);
}
return result;
}
@end
@implementation DoricSliderNode
@ -109,6 +118,12 @@ - (void)blendView:(UICollectionView *)view forPropName:(NSString *)name propValu
} else if ([prop isKindOfClass:NSString.class]) {
self.slideStyle = prop;
}
} else if ([@"slidePosition" isEqualToString:name]) {
self.slidePosition = [prop unsignedIntegerValue];
if (self.view.width > 0 && ((NSUInteger) self.view.contentOffset.x / self.view.width) == self.slidePosition) {
} else {
self.needResetScroll = YES;
}
} else {
[super blendView:view forPropName:name propValue:prop];
}
@ -144,10 +159,8 @@ - (void)afterBlended:(NSDictionary *)props {
if (needToScroll) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(_self) self = _self;
[self.view reloadData];
int position = self.loop ? 1 : 0;
NSUInteger position = (self.loop ? 1 : 0) + self.slidePosition;
if (self.view.width == 0) {
self.needResetScroll = true;
} else {
@ -197,6 +210,18 @@ - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collection
[node blend:props];
[node.view.doricLayout apply:CGSizeMake(collectionView.width, collectionView.height)];
[node requestLayout];
BOOL needLayout = NO;
if (self.view.doricLayout.widthSpec == DoricLayoutFit && self.view.width < node.view.width) {
self.view.width = node.view.width;
needLayout = YES;
}
if (self.view.doricLayout.heightSpec == DoricLayoutFit && self.view.height < node.view.height) {
self.view.height = node.view.height;
needLayout = YES;
}
if (needLayout) {
[self scheduleLayout];
}
return cell;
} @catch (NSException *exception) {
[self.doricContext.driver.registry onException:exception inContext:self.doricContext];
@ -204,6 +229,22 @@ - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collection
}
}
- (void)scheduleLayout {
if (self.scheduledLayout) {
return;
}
self.scheduledLayout = YES;
dispatch_async(dispatch_get_main_queue(), ^{
self.scheduledLayout = NO;
DoricSuperNode *node = self.superNode;
while (node.superNode != nil) {
node = node.superNode;
}
[node requestLayout];
[self.view reloadData];
});
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
@ -361,6 +402,7 @@ - (void)reset {
self.slideStyle = nil;
self.minScale = .618f;
self.maxScale = 1.f;
self.slidePosition = 0;
}
- (void)subNodeContentChanged:(DoricViewNode *)subNode {