11import React , { PureComponent } from 'react' ;
22import PropTypes from 'prop-types' ;
33
4- const IMG_RESET_SIZE = {
5- width : 0 ,
6- height : 0 ,
7- left : 0 ,
8- top : 0 ,
4+ /**
5+ *
6+ * @param {number } value
7+ * @param {number } min
8+ * @param {number } max
9+ */
10+
11+ function setScope ( value , min , max ) {
12+ if ( value < min ) { return min }
13+ if ( value > max ) { return max }
14+ return value ;
915}
1016
1117/**
@@ -25,19 +31,16 @@ const IMG_RESET_SIZE = {
2531 * 对应关系: newPointLeft = zoom * oldPointLeft
2632 * newPointTop = zoom * oldPointTop
2733 *
28- * 坐标位置:left = (newPointLeft - oldPointLeft) = (zoom - 1) * oldPointLeft
29- * top = (newPointTop - oldPointTop) = (zoom - 1) * oldPointTop
34+ * 坐标位置:left = startLeft + (newPointLeft - oldPointLeft) = (zoom - 1) * oldPointLeft
35+ * top = startTop + (newPointTop - oldPointTop) = (zoom - 1) * oldPointTop
3036 */
3137
32- function setScope ( value , min , max ) {
33- if ( value < min ) { return min }
34- if ( value > max ) { return max }
35- return value ;
36- }
37-
3838class ImageContainer extends PureComponent {
3939 static propTypes = {
4040 maxZoomNum : PropTypes . number ,
41+ handleStart : PropTypes . func ,
42+ handleMove : PropTypes . func ,
43+ handleEnd : PropTypes . func
4144 }
4245
4346 static defaultProps = {
@@ -66,21 +69,25 @@ class ImageContainer extends PureComponent {
6669
6770 switch ( event . touches . length ) {
6871 case 1 :
69- // event.preventDefault();
70- // event.stopPropagation();
72+ event . preventDefault ( ) ;
7173 let targetEvent = event . touches [ 0 ] ;
7274 this . startX = targetEvent . clientX ;
7375 this . startY = targetEvent . clientY ;
7476
7577 this . startLeft = this . state . left ;
7678 this . startTop = this . state . top ;
79+
80+ if ( this . state . width === this . originWidth ) {
81+ this . callHandleStart ( )
82+ }
7783 break ;
7884
7985 case 2 : //两个手指
8086 event . preventDefault ( ) ;
8187 event . stopPropagation ( ) ;
8288
83- this . isTwoFinger = true ;
89+ //设置手双指模式
90+ this . isTwoFingerMode = true ;
8491
8592 //计算两个手指中间点屏幕上的坐标
8693 const middlePointClientLeft = Math . abs ( Math . round ( ( event . touches [ 0 ] . clientX + event . touches [ 1 ] . clientX ) / 2 ) ) ;
@@ -95,60 +102,59 @@ class ImageContainer extends PureComponent {
95102 //计算手指中间点在图片上的位置(坐标值)
96103 this . oldPointLeft = middlePointClientLeft - this . startLeft ;
97104 this . oldPointTop = middlePointClientTop - this . startTop ;
98- console . info ( "this.oldPointLeft = %s; this.oldPointTop = %s" , this . oldPointLeft , this . oldPointTop ) ;
99105
100106 var dx = event . touches [ 0 ] . clientX - event . touches [ 1 ] . clientX ;
101107 var dy = event . touches [ 0 ] . clientY - event . touches [ 1 ] . clientY ;
102108 this . _touchZoomDistanceStart = this . _touchZoomDistanceEnd = Math . sqrt ( dx * dx + dy * dy ) ;
103-
104- console . info ( "this._touchZoomDistanceStart = " , this . _touchZoomDistanceStart ) ;
105109 break ;
106-
107110 default :
108111 break ;
109112 }
110113 }
111114
112115 handleTouchMove = ( event ) => {
113- // console.info("handleTouchMove = %s",event.touches[ 0 ].clientX)
114-
115116 switch ( event . touches . length ) {
116117 case 1 :
117118 let targetEvent = event . touches [ 0 ] ,
118119 diffX = targetEvent . clientX - this . startX ,
119120 diffY = targetEvent . clientY - this . startY ;
120121
121-
122- let top = ( this . props . screenHeight - this . state . height ) / 2 ,
123- left = setScope ( this . startLeft + diffX , ( this . originWidth - this . state . width ) , 0 ) ;
122+ //图片宽度等于初始宽度,直接调用 handleMove 函数
123+ if ( this . state . width === this . originWidth ) {
124+ if ( this . props . handleMove ) {
125+ this . props . handleMove ( diffX ) ;
126+ }
127+ } else {
128+ let top = ( this . props . screenHeight - this . state . height ) / 2 ,
129+ left = this . startLeft + diffX ;
124130
125- if ( this . state . height > this . props . screenHeight ) {
126- top = setScope ( this . startTop + diffY , ( this . props . screenHeight - this . state . height ) , 0 ) ;
127- }
131+ if ( this . state . height > this . props . screenHeight ) {
132+ top = setScope ( this . startTop + diffY , ( this . props . screenHeight - this . state . height ) , 0 ) ;
133+ }
134+
135+ if ( left < this . originWidth - this . state . width ) {
136+ this . callHandleStart ( ) ;
137+ if ( this . props . handleMove ) {
138+ this . props . handleMove ( left + this . state . width - this . originWidth ) ;
139+ }
140+ } else if ( left > 0 ) {
141+ this . callHandleStart ( ) ;
142+ if ( this . props . handleMove ) {
143+ this . props . handleMove ( left ) ;
144+ }
145+ }
146+
147+ left = setScope ( left , this . originWidth - this . state . width , 0 ) ;
148+
149+ console . info ( "this.startX = %s, this.startY = %s, this.startLeft = %s, this.startTop = %s, diffX = %s, diffY = %s" , this . startX , this . startY , this . startLeft , this . startTop , diffX , diffY ) ;
150+ this . setState ( {
151+ left,
152+ top
153+ } )
128154
129- console . info ( "this.startX = %s, this.startY = %s, this.startLeft = %s, this.startTop = %s, diffX = %s, diffY = %s" , this . startX , this . startY , this . startLeft , this . startTop , diffX , diffY ) ;
130- this . setState ( {
131- left,
132- top
133- } )
134- if ( this . startLeft + diffX < 0 && this . startLeft + diffX > this . originWidth - this . state . width ) {
135- event . preventDefault ( ) ;
136- event . stopPropagation ( ) ;
137155 }
138- // this.setState((prevState, props) => {
139- // let top = (props.screenHeight - prevState.height)/2,
140- // left = setScope(this.startLeft + diffX, ( this.originWidth - prevState.width ), 0 );
141-
142- // if(prevState.height > props.screenHeight){
143- // top = setScope(this.startTop + diffY, ( props.screenHeight - prevState.height ), 0 );
144- // }
145-
146- // console.info("left = %s ; top = %s", left, top);
147- // return {
148- // left,
149- // top
150- // }
151- // })
156+
157+ event . preventDefault ( ) ;
152158 break ;
153159
154160 case 2 : //两个手指
@@ -185,8 +191,8 @@ class ImageContainer extends PureComponent {
185191
186192 handleTouchEnd = ( event ) => {
187193 console . info ( "handleTouchEnd" , event . touches . length ) ;
188- if ( this . isTwoFinger ) {
189- this . isTwoFinger = false ;
194+ if ( this . isTwoFingerMode ) { //双指操作结束
195+ this . isTwoFingerMode = false ;
190196 let left , top , width , height ;
191197
192198 width = setScope ( this . state . width , this . originWidth , this . props . maxZoomNum * this . originWidth ) ;
@@ -220,6 +226,25 @@ class ImageContainer extends PureComponent {
220226 this . startTop = top ;
221227 console . info ( "this.startX = %s, this.startY = %s, this.startLeft = %s, this.startTop = %s" , this . startX , this . startY , this . startLeft , this . startTop ) ;
222228 }
229+ } else { //单指结束(ontouchend)
230+ this . callHandleEnd ( ) ;
231+ }
232+ event . preventDefault ( ) ;
233+ }
234+
235+ callHandleStart = ( ) => {
236+ if ( ! this . isCalledHandleStart ) {
237+ this . isCalledHandleStart = true ;
238+ if ( this . props . handleStart ) {
239+ this . props . handleStart ( ) ;
240+ }
241+ }
242+ }
243+
244+ callHandleEnd = ( ) => {
245+ this . isCalledHandleStart = false ;
246+ if ( this . props . handleEnd ) {
247+ this . props . handleEnd ( ) ;
223248 }
224249 }
225250
0 commit comments