Skip to content

Commit d976947

Browse files
committed
完成pagingEnabled和pageSize属性
1 parent 22e1ed9 commit d976947

File tree

8 files changed

+94
-43
lines changed

8 files changed

+94
-43
lines changed

Examples/BasicPropsTest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: 石破天惊
33
* @email: shanshang130@gmail.com
44
* @Date: 2021-07-16 17:29:37
5-
* @LastEditTime: 2021-07-19 18:00:03
5+
* @LastEditTime: 2021-07-19 23:48:30
66
* @LastEditors: 石破天惊
77
* @Description:
88
*/
@@ -28,7 +28,7 @@ export class BasicPropsTest extends React.Component {
2828
<SpringScrollView
2929
{...this.state}
3030
pagingEnabled
31-
pageSize={{width:400,height:200}}
31+
pageSize={{width:200,height:200}}
3232
>
3333
{Object.keys(this.state).map((key) => (
3434
<Row

src/SpringScrollView.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,5 +545,7 @@ export class SpringScrollView extends React.PureComponent<SpringScrollViewPropTy
545545
showsHorizontalScrollIndicator: true,
546546
initialContentOffset: {x: 0, y: 0},
547547
alwaysBounceVertical: true,
548+
pagingEnabled: false,
549+
pageSize: {width: 0, height: 0},
548550
};
549551
}

src/SpringScrollViewNative.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/*
2+
* @Author: 石破天惊
3+
* @email: shanshang130@gmail.com
4+
* @Date: 2021-07-15 17:11:44
5+
* @LastEditTime: 2021-07-19 20:34:28
6+
* @LastEditors: 石破天惊
7+
* @Description:
8+
*/
19
/**
210
* Author: Shi(bolan0000@icloud.com)
311
* Date: 2019/1/17
@@ -19,6 +27,11 @@ export class SpringScrollViewNativeAdapter extends React.Component {
1927
return (
2028
<SpringScrollViewNative
2129
{...this.props}
30+
pagingEnabled={Platform.select({
31+
ios: false,
32+
android: this.props.pagingEnabled,
33+
})}
34+
pagingEnabledB={this.props.pagingEnabled}
2235
ref={(ref) => (this._scrollViewRef = ref)}
2336
onTouchStart={(e) =>
2437
Platform.OS === 'ios' &&
@@ -49,17 +62,13 @@ export class SpringScrollViewNativeAdapter extends React.Component {
4962
}
5063

5164
attachScrollNativeEvent(offset) {
52-
return Animated.attachNativeEvent(
53-
this._scrollViewRef,
54-
'onScroll',
55-
[
56-
{
57-
nativeEvent: {
58-
contentOffset: offset,
59-
},
65+
return Animated.attachNativeEvent(this._scrollViewRef, 'onScroll', [
66+
{
67+
nativeEvent: {
68+
contentOffset: offset,
6069
},
61-
],
62-
);
70+
},
71+
]);
6372
}
6473
}
6574

src/Types.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export interface Offset {
2121
y: number
2222
}
2323

24+
export interface Size {
25+
width: number;
26+
height: number;
27+
}
28+
2429
export interface NativeContentOffset {
2530
x?: Animated.Value,
2631
y?: Animated.Value
@@ -44,6 +49,8 @@ export interface SpringScrollViewPropType extends ViewProps {
4449
contentStyle?: ViewStyle,
4550
bounces?: boolean,
4651
scrollEnabled?: boolean,
52+
pagingEnabled?: boolean,
53+
pageSize?: Size,
4754
directionalLockEnabled?: boolean,
4855
initialContentOffset?: Offset,
4956
showsVerticalScrollIndicator?: boolean,

src/android/src/main/java/com/bolan9999/SpringScrollView.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
119119
sendEvent("onCustomScrollBeginDrag", null);
120120
}
121121
dragging = true;
122-
NativeGestureUtil.notifyNativeGestureStarted(this, ev);
123-
ReactScrollViewHelper.emitScrollBeginDragEvent(this);
124122
getParent().requestDisallowInterceptTouchEvent(true);
125123
}
126124
break;
@@ -145,8 +143,8 @@ private boolean shouldDrag(MotionEvent ev) {
145143
if (!scrollEnabled) return false;
146144
if (beginPoint.x == 0 && beginPoint.y == 0) return false;
147145
if (dragging) return true;
148-
if (canHorizontalScroll()) {
149-
return Math.abs(ev.getX() - beginPoint.x) > PixelUtil.toPixelFromDIP(10);
146+
if (canHorizontalScroll() && Math.abs(ev.getX() - beginPoint.x) > PixelUtil.toPixelFromDIP(10)) {
147+
return true;
150148
}
151149
return Math.abs(ev.getY() - beginPoint.y) > PixelUtil.toPixelFromDIP(5);
152150
}
@@ -216,15 +214,12 @@ private void onUp(MotionEvent evt) {
216214
}
217215

218216
private void beginInnerHorizontalAnimation(final float initialVelocity) {
219-
if (Math.abs(initialVelocity) < 0.1f) {
220-
return;
221-
}
222217
if (!momentumScrolling) {
223218
momentumScrolling = true;
224219
sendEvent("onCustomMomentumScrollBegin", null);
225220
}
226221
final long beginTimeInterval = System.currentTimeMillis();
227-
float dampingCoefficient = pagingEnabled ? 0.99f : 0.997f;
222+
final float dampingCoefficient = pagingEnabled ? 0.99f : 0.997f;
228223
float end;
229224
float displacement = 0;
230225
float v = initialVelocity;
@@ -236,7 +231,7 @@ private void beginInnerHorizontalAnimation(final float initialVelocity) {
236231
}
237232
if (pagingEnabled) {
238233
duration = 500;
239-
end = Math.round((contentOffset.x - displacement) / getPageSize().width) * getPageSize().width;
234+
end = Math.round((contentOffset.x - displacement) / getPageWidth()) * getPageWidth();
240235
} else {
241236
end = contentOffset.x - displacement;
242237
}
@@ -248,16 +243,15 @@ public void onDecelerateUpdate(DecelerateAnimation animation, float value) {
248243
long interval = System.currentTimeMillis() - beginTimeInterval;
249244
float v = initialVelocity;
250245
while (interval-- > 0) {
251-
v *= 0.997f;
246+
v *= dampingCoefficient;
252247
}
253248
animation.cancel();
254249
beginOuterHorizontalAnimation(v);
255250
}
256251
}
257252

258253
@Override
259-
public void onDecelerateEnd(DecelerateAnimation animation) {
260-
}
254+
public void onDecelerateEnd(DecelerateAnimation animation) {}
261255
});
262256
horizontalAnimation.start();
263257
}
@@ -293,17 +287,13 @@ private void beginHorizontalReboundAnimation() {
293287
}
294288

295289
private void beginInnerAnimation(final float initialVelocity) {
296-
if (Math.abs(initialVelocity) < 0.1f) {
297-
onVerticalAnimationEnd();
298-
return;
299-
}
300290
if (!momentumScrolling) {
301291
momentumScrolling = true;
302292
sendEvent("onCustomMomentumScrollBegin", null);
303293
}
304294
final long beginTimeInterval = System.currentTimeMillis();
305295
float end;
306-
float dampingCoefficient = pagingEnabled ? 0.99f : 0.997f;
296+
final float dampingCoefficient = pagingEnabled ? 0.99f : 0.997f;
307297
float displacement = 0;
308298
float v = initialVelocity;
309299
int duration = 0;
@@ -314,7 +304,7 @@ private void beginInnerAnimation(final float initialVelocity) {
314304
}
315305
if (pagingEnabled) {
316306
duration = 500;
317-
end = Math.round((contentOffset.y - displacement) / getPageSize().height) * getPageSize().height;
307+
end = Math.round((contentOffset.y - displacement) / getPageHeight()) * getPageHeight();
318308
} else {
319309
end = contentOffset.y - displacement;
320310
}
@@ -326,7 +316,7 @@ public void onDecelerateUpdate(DecelerateAnimation animation, float value) {
326316
long interval = System.currentTimeMillis() - beginTimeInterval;
327317
float v = initialVelocity;
328318
while (interval-- > 0) {
329-
v *= 0.997f;
319+
v *= dampingCoefficient;
330320
}
331321
animation.cancel();
332322
beginOuterAnimation(v);
@@ -393,7 +383,7 @@ private boolean cancelAllAnimations() {
393383
if (horizontalAnimation.animating) {
394384
cancelHorizontal = horizontalAnimation.cancel();
395385
}
396-
return cancelVertical || cancelVertical;
386+
return cancelVertical || cancelHorizontal;
397387
}
398388

399389

@@ -601,8 +591,12 @@ public void setPageSize(float width, float height) {
601591
this.pageSize.height = height;
602592
}
603593

604-
public Size getPageSize() {
605-
return pageSize.width == 0 && pageSize.height == 0 ? contentSize : pageSize;
594+
private float getPageWidth() {
595+
return pageSize.width <= 0? size.width : pageSize.width;
596+
}
597+
598+
private float getPageHeight() {
599+
return pageSize.height <= 0? size.height : pageSize.height;
606600
}
607601

608602
private boolean overshootHead() {

src/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ declare module 'react-native-spring-scrollview' {
9191
contentStyle?: ViewStyle;
9292
bounces?: boolean;
9393
scrollEnabled?: boolean;
94+
pagingEnabled?: boolean;
95+
pageSize?: Size;
9496
directionalLockEnabled?: boolean;
9597
initialContentOffset?: Offset;
9698
showsVerticalScrollIndicator?: boolean;

src/ios/SpringScrollView/STSpringScrollView.m

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@
1212
@interface STSpringScrollView ()
1313
@property(nonatomic, assign) float refreshHeaderHeight;
1414
@property(nonatomic, assign) float loadingFooterHeight;
15-
//@property(nonatomic, assign) UIEdgeInsets orgInsets;
1615
@property(nonatomic, copy) NSString *refreshStatus;
1716
@property(nonatomic, copy) NSString *loadingStatus;
1817
@property(nonatomic, copy) NSDictionary *initialContentOffset;
18+
@property(nonatomic, copy) NSDictionary *pageSize;
1919
@property(nonatomic, assign) BOOL allLoaded;
2020
@property(nonatomic, assign) BOOL initialed;
21+
@property(nonatomic, assign) BOOL pagingEnabledB;
2122
@end
2223

23-
@implementation STSpringScrollView
24-
24+
@implementation STSpringScrollView{
25+
NSHashTable *_scrollListeners;
26+
}
2527
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher{
2628
if (self=[super initWithEventDispatcher:eventDispatcher]) {
2729
self.refreshStatus = self.loadingStatus = @"waiting";
@@ -37,6 +39,16 @@ - (void)setInitialContentOffset:(NSDictionary *)initialContentOffset{
3739
}
3840
}
3941

42+
- (float) getPageWidth{
43+
CGFloat width = [self.pageSize[@"width"] floatValue];
44+
return width<=0?self.scrollView.frame.size.width:width;
45+
}
46+
47+
- (float) getPageHeight{
48+
CGFloat height = [self.pageSize[@"height"] floatValue];
49+
return height<=0?self.scrollView.frame.size.height:height;
50+
}
51+
4052
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
4153
CGSize size = [change[NSKeyValueChangeNewKey] CGSizeValue];
4254
float x = [[self.initialContentOffset objectForKey:@"x"] floatValue];
@@ -51,23 +63,46 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
5163

5264
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
5365
[super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
54-
// if ([self shouldRefresh]) {
55-
// self.refreshStatus = @"refreshing";
56-
// [self.scrollView setContentInset:UIEdgeInsetsMake(self.refreshHeaderHeight, 0, 0, 0)];
57-
// } else
5866
if ([self shouldLoad]) {
5967
self.loadingStatus = @"loading";
6068
CGFloat fill = .0f;
6169
if(self.scrollView.frame.size.height>self.scrollView.contentSize.height){
6270
fill=self.scrollView.frame.size.height-self.scrollView.contentSize.height;
6371
}
6472
[self.scrollView setContentInset:UIEdgeInsetsMake(0, 0, self.loadingFooterHeight+fill, 0)];
65-
}
66-
else if ([self hitRefreshStatus:@[@"rebound"]] && !UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, scrollView.contentInset)){
73+
} else if ([self hitRefreshStatus:@[@"rebound"]] && !UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, scrollView.contentInset)){
6774
[self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
6875
}
6976
}
7077

78+
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
79+
withVelocity:(CGPoint)velocity
80+
targetContentOffset:(inout CGPoint *)targetContentOffset
81+
{
82+
if (self.pagingEnabledB) {
83+
float displacement = 0;
84+
float v = -velocity.x;
85+
int duration = 0;
86+
while (fabsf(v) > 0.01f) {
87+
displacement += v;
88+
v *= 0.99f;
89+
duration++;
90+
}
91+
targetContentOffset->x = round((self.scrollView.contentOffset.x - displacement)/[self getPageWidth])*[self getPageWidth];
92+
v = -velocity.y;
93+
duration = 0;
94+
displacement = 0;
95+
while (fabsf(v) > 0.01f) {
96+
displacement += v;
97+
v *= 0.99f;
98+
duration++;
99+
}
100+
targetContentOffset->y = round((self.scrollView.contentOffset.y - displacement)/[self getPageHeight])*[self getPageHeight];
101+
return;
102+
}
103+
[super scrollViewWillEndDragging:scrollView withVelocity:velocity targetContentOffset:targetContentOffset];
104+
}
105+
71106
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
72107
if ([self shouldPulling]) {
73108
self.refreshStatus = @"pulling";

src/ios/SpringScrollView/STSpringScrollViewManager.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ @implementation STSpringScrollViewManager
2222
RCT_EXPORT_VIEW_PROPERTY(loadingFooterHeight, float)
2323
RCT_EXPORT_VIEW_PROPERTY(initialContentOffset, id)
2424
RCT_EXPORT_VIEW_PROPERTY(allLoaded, BOOL)
25+
RCT_EXPORT_VIEW_PROPERTY(pageSize, id)
26+
RCT_EXPORT_VIEW_PROPERTY(pagingEnabledB, BOOL)
2527

2628
- (UIView *)view
2729
{

0 commit comments

Comments
 (0)