我正试图通过PanResponder处理React Native中的长按。在一次体面的搜索后,我无法找到如何以正确的方式做到这一点,所以我在这里问。这个想法是在检测到屏幕上的长按(点击)时执行代码。 我达到了这样的目的:
public class ScriptCreator {
public static void main(String[] args) throws IOException {
#Choose the CSV file that I am importing the data from
String fName = "C:\\Users\\MyUser\\Downloads\\CurrentApplications (1).csv";
String thisLine;
int count = 0;
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
int i = 0;
#Prints the List of names in the CSV file
while((thisLine = myInput.readLine()) != null){
String strar[] = thisLine.split(",");
Printer(strar[0]);
}
}
public static void Printer(String arg) throws IOException{
#Want to pull from the String strar[0] from above
#Says that it cannot be resolved to a variable
String name = arg;
String direc = "C:/Users/MyUser/Documents/";
String path = "C:/Users/MyUser/Documents";
Iterable<String> lines = Arrays.asList("LOGIN -acceptssl ServerName","N " + name + " " + direc ,"cd " + name,"import " + path + "*.ppf" + " true","scan", "publishassessase -aseapplication " + name,"removeassess *","del " + name );
Path file = Paths.get(name + ".txt");
Files.write(file, lines, Charset.forName("UTF-8"));
}
}
这是处理长按的正确方法还是有更好的方法?
答案 0 :(得分:8)
我最终使用setTimeout
执行此功能。这是一个代码,它具有检测屏幕的哪个部分被长按(左或右)的功能:
handlePanResponderGrant(e, gestureState) {
console.log('Start of touch');
this.long_press_timeout = setTimeout(function(){
if (gestureState.x0 <= width/2 )
{
AlertIOS.alert(
'Left',
'Long click on the left side detected',
[
{text: 'Tru dat'}
]
);
}
else {
AlertIOS.alert(
'Right',
'So you clicked on the right side?',
[
{text: 'Indeed'}
]
);
}
},
LONG_PRESS_MIN_DURATION);
}
handlePanResponderMove(e, gestureState) {
clearTimeout(this.long_press_timeout);
}
handlePanResponderRelease(e, gestureState){
clearTimeout(this.long_press_timeout);
console.log('Touch released');
}
handlePanResponderEnd(e, gestureState) {
clearTimeout(this.long_press_timeout);
console.log('Finger pulled up from the image');
}
答案 1 :(得分:2)
我在ScrollView中有Carousel,我想知道用户在def stringPipeline(string1: String) = (string2: String) => {
string1.foldLeft(string2)((s: String, c: Char) =>
c match {
case 'u' => s.toUpperCase
case 'r' => s.reverse
case _ => s
}
)
}
的项目上按下了什么。感谢@Alexander Netsov,我最终做到了这一点。
Carousel
垂直this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: () => true,
onMoveShouldSetPanResponder: () => false,
onPanResponderGrant: (e, gestureState) => {
this.onLongPressTimeout = setTimeout(() => {
console.log("ON LONG PRESS", gestureState);
}, LONG_PRESS_DELAY);
},
onPanResponderRelease: () => {
clearTimeout(this.onLongPressTimeout);
},
onPanResponderTerminate: () => {
clearTimeout(this.onLongPressTimeout);
},
onShouldBlockNativeResponder: () => false,
onPanResponderTerminationRequest: () => true
});
,横向ScrollView
和Carousel
,在Android上都运行良好。
注意:它未在iOS上测试