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 @end
@implementation DoricNestedSliderView @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 @end
@interface DoricNestedSliderNode () <UIScrollViewDelegate> @interface DoricNestedSliderNode () <UIScrollViewDelegate>

View File

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