feat:iOS add Slider event

This commit is contained in:
pengfei.zhou 2019-12-06 16:34:20 +08:00
parent 7c5ac7b5b2
commit 187dd9e0c3

View File

@ -36,6 +36,7 @@ @interface DoricSliderNode () <UICollectionViewDataSource, UICollectionViewDeleg
@property(nonatomic, assign) NSUInteger itemCount; @property(nonatomic, assign) NSUInteger itemCount;
@property(nonatomic, assign) NSUInteger batchCount; @property(nonatomic, assign) NSUInteger batchCount;
@property(nonatomic, copy) NSString *onPageSelectedFuncId; @property(nonatomic, copy) NSString *onPageSelectedFuncId;
@property(nonatomic, assign) NSUInteger lastPosition;
@end @end
@interface DoricSliderView : UICollectionView @interface DoricSliderView : UICollectionView
@ -201,22 +202,22 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSUInteger pageIndex = (NSUInteger) (scrollView.contentOffset.x / scrollView.width); NSUInteger pageIndex = (NSUInteger) (scrollView.contentOffset.x / scrollView.width);
scrollView.contentOffset = CGPointMake(pageIndex * scrollView.width, scrollView.contentOffset.y); scrollView.contentOffset = CGPointMake(pageIndex * scrollView.width, scrollView.contentOffset.y);
if (self.onPageSelectedFuncId && self.onPageSelectedFuncId.length > 0) { if (self.onPageSelectedFuncId && self.onPageSelectedFuncId.length > 0) {
[self callJSResponse:self.onPageSelectedFuncId, @(pageIndex), nil]; if (pageIndex != self.lastPosition) {
[self callJSResponse:self.onPageSelectedFuncId, @(pageIndex), nil];
} else {
self.lastPosition = pageIndex;
}
} }
} }
- (void)slidePage:(NSDictionary *)params withPromise:(DoricPromise *)promise { - (void)slidePage:(NSDictionary *)params withPromise:(DoricPromise *)promise {
NSUInteger pageIndex = [params[@"page"] unsignedIntegerValue]; NSUInteger pageIndex = [params[@"page"] unsignedIntegerValue];
BOOL smooth = [params[@"smooth"] boolValue]; BOOL smooth = [params[@"smooth"] boolValue];
if (smooth) { [self.view setContentOffset:CGPointMake(pageIndex * self.view.width, self.view.contentOffset.y) animated:smooth];
[UIView animateWithDuration:.3f animations:^{ [promise resolve:nil];
self.view.contentOffset = CGPointMake(pageIndex * self.view.width, self.view.contentOffset.y); self.lastPosition = pageIndex;
} if (self.onPageSelectedFuncId && self.onPageSelectedFuncId.length > 0) {
completion:^(BOOL finished) { [self callJSResponse:self.onPageSelectedFuncId, @(pageIndex), nil];
[promise resolve:nil];
}];
} else {
self.view.contentOffset = CGPointMake(pageIndex * self.view.width, self.view.contentOffset.y);
} }
} }