1. 程式人生 > >react-native 新聞標籤排序編輯功能

react-native 新聞標籤排序編輯功能

1.類似頭條和UC瀏覽器的標籤編輯。

2.核心程式碼(PanResponder手勢系統的使用

	this._panResponder = PanResponder.create({
			onStartShouldSetPanResponder: (evt, gestureState) => {
				return gestureState.dx !== 0 || gestureState.dx !== 0;
			},
			onMoveShouldSetPanResponder: (evt, gestureState) => true,
			onPanResponderGrant: (evt, gestureState) => {
				const { pageX, pageY } = evt.nativeEvent;

				this.topIndex = Math.floor((pageY - this.offset) / (this._width / 2));
				this.leftIndex = Math.floor(pageX / this._width);
				this.index = this.topIndex * 4 + this.leftIndex;

				this.prev_left = this._width * this.leftIndex;
				this.prev_top = this._width / 2 * this.topIndex;
			},
			onPanResponderMove: (evt, gestureState) => {
				//Alert.alert(JSON.stringify(this.index));
				if (this.index > 0) {
					this.left = this.prev_left + gestureState.dx;
					this.top = this.prev_top + gestureState.dy;
					let box = this.refs[this.items[this.index].id];
					box.setNativeProps({
						style: { top: this.top, left: this.left }
					});
				}
			},

			onPanResponderTerminationRequest: (evt, gestureState) => true,
			onPanResponderRelease: (evt, gestureState) => {
				if (this.index > 0) {
					const { pageX, pageY } = evt.nativeEvent;
					this.finalTopIndex = Math.floor((pageY - this.offset) / (this._width / 2));
					this.finalLeftIndex = Math.floor(pageX / this._width);
					let index = this.finalTopIndex * 4 + this.finalLeftIndex;

					this.prev_left = this._width * this.finalTopIndex;
					this.prev_top = this._width / 2 * this.finalTopIndex;

					if (index > 0 && this.items[index]) {
						if (index > this.index) {
							//往後移動
							for (let i = this.index; i < index; i++) {
								let box2 = this.refs[this.items[i + 1].id];
								let top2 = Math.floor(i / 4) * (this._width / 2);
								let left2 = (i % 4) * this._width;
								//LayoutAnimation.linear();
								LayoutAnimation.configureNext(
									LayoutAnimation.create(
										200,
										LayoutAnimation.Types.linear,
										LayoutAnimation.Properties.scaleXY
									)
								);
								box2.setNativeProps({
									style: {
										top: top2,
										left: left2
									}
								});
							}
							let box1 = this.refs[this.items[this.index].id];
							let top1 = Math.floor(index / 4) * (this._width / 2);
							let left1 = (index % 4) * this._width;

							box1.setNativeProps({
								style: {
									top: top1,
									left: left1
								}
							});
							let temp = this.items[this.index];
							for (let i = this.index; i < index; i++) {
								this.items[i] = this.items[i + 1];
							}
							this.items[index] = temp;
						} else if (index < this.index) {
							//往前移動
							for (let i = this.index; i > index; i--) {
								let box2 = this.refs[this.items[i - 1].id];
								let top2 = Math.floor(i / 4) * (this._width / 2);
								let left2 = (i % 4) * this._width;
								//LayoutAnimation.linear();
								LayoutAnimation.configureNext(
									LayoutAnimation.create(
										200,
										LayoutAnimation.Types.linear,
										LayoutAnimation.Properties.scaleXY
									)
								);
								box2.setNativeProps({
									style: {
										top: top2,
										left: left2
									}
								});
							}
							let box1 = this.refs[this.items[this.index].id];
							let top1 = Math.floor(index / 4) * (this._width / 2);
							let left1 = (index % 4) * this._width;

							box1.setNativeProps({
								style: {
									top: top1,
									left: left1
								}
							});
							let temp = this.items[this.index];
							for (let i = this.index; i > index; i--) {
								this.items[i] = this.items[i - 1];
							}
							this.items[index] = temp;
						}
					} else {
						//移出範圍,則重新回到原始位置
						let box1 = this.refs[this.items[this.index].id];
						let top1 = Math.floor(this.index / 4) * (this._width / 2);
						let left1 = (this.index % 4) * this._width;
						LayoutAnimation.linear();
						box1.setNativeProps({
							style: {
								top: top1,
								left: left1
							}
						});
						LayoutAnimation.configureNext(LayoutAnimation.Presets.spring); //系統自帶
					}
				}
			},
			//onPanResponderTerminate: (evt, gestureState) => this._release(evt, gestureState),
			onShouldBlockNativeResponder: (event, gestureState) => true
		});


3.git原始碼https://github.com/nfq6612/reactnative_labelEdit.git

4.(Xmarin c#版和 android studio  java版)我也有,有需要可以回覆!

原文地址:http://blog.csdn.net/nfq6612/article/details/78675515