She*_*lam 352 iphone cocoa-touch objective-c gesture-recognition uigesturerecognizer
我正在检测用户是否已按下2秒钟:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 2.0;
[self addGestureRecognizer:longPress];
[longPress release];
Run Code Online (Sandbox Code Playgroud)
这就是我处理长按的方式:
-(void)handleLongPress:(UILongPressGestureRecognizer*)recognizer{
NSLog(@"double oo");
}
Run Code Online (Sandbox Code Playgroud)
当我按下超过2秒钟时,文本"double oo"被打印两次.为什么是这样?我该怎么办?
joe*_*elm 671
UILongPressGestureRecognizer是一个连续的事件识别器.你必须查看状态,看看这是事件的开始,中间还是结束,并采取相应的行动.也就是说,您可以在开始后丢弃所有事件,或者只根据需要查看运动.从 类参考:
长按手势是连续的.当指定时间段(minimumPressDuration)按下允许的手指数(numberOfTouchesRequired)并且触摸不超出允许的移动范围(allowableMovement)时,手势开始(UIGestureRecognizerStateBegan).每当手指移动时,手势识别器转换到改变状态,并且当任何手指被抬起时,手势识别器结束(UIGestureRecognizerStateEnded).
现在你可以追踪像这样的状态
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"UIGestureRecognizerStateEnded");
//Do Whatever You want on End of Gesture
}
else if (sender.state == UIGestureRecognizerStateBegan){
NSLog(@"UIGestureRecognizerStateBegan.");
//Do Whatever You want on Began of Gesture
}
}
Run Code Online (Sandbox Code Playgroud)
小智 116
要检查UILongPressGestureRecognizer的状态,只需在选择器方法上添加if语句:
- (void)handleLongPress:(UILongPressGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"Long press Ended");
} else if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"Long press detected.");
}
}
Run Code Online (Sandbox Code Playgroud)
Pau*_*olt 74
您需要检查正确的状态,因为每个州都有不同的行为.很可能你会需要UIGestureRecognizerStateBegan
国家UILongPressGestureRecognizer
.
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[myView addGestureRecognizer:longPress];
[longPress release];
Run Code Online (Sandbox Code Playgroud)
...
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
if(UIGestureRecognizerStateBegan == gesture.state) {
// Called on start of gesture, do work here
}
if(UIGestureRecognizerStateChanged == gesture.state) {
// Do repeated work here (repeats continuously) while finger is down
}
if(UIGestureRecognizerStateEnded == gesture.state) {
// Do end work here when finger is lifted
}
}
Run Code Online (Sandbox Code Playgroud)
Raj*_*han 18
Objective-C的
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"Long press Ended");
} else if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"Long press detected.");
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 2.2:
func handleLongPress(sender:UILongPressGestureRecognizer) {
if (sender.state == UIGestureRecognizerState.Ended) {
print("Long press Ended");
} else if (sender.state == UIGestureRecognizerState.Began) {
print("Long press detected.");
}
}
Run Code Online (Sandbox Code Playgroud)
Raj*_*Raj 13
以下是在Swift中处理它的方法:
func longPress(sender:UILongPressGestureRecognizer!) {
if (sender.state == UIGestureRecognizerState.Ended) {
println("Long press Ended");
} else if (sender.state == UIGestureRecognizerState.Began) {
println("Long press detected.");
}
}
Run Code Online (Sandbox Code Playgroud)
foz*_*glu 13
Swift 3.0:
func handleLongPress(sender: UILongPressGestureRecognizer) {
if sender.state == .ended {
print("Long press Ended")
} else if sender.state == .began {
print("Long press detected")
}
Run Code Online (Sandbox Code Playgroud)
您的手势处理程序接收每个手势状态的调用.所以你需要检查每个状态并将你的代码置于必需的状态.
人们必须更喜欢在if-else上使用switch-case:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[myView addGestureRecognizer:longPress];
[longPress release];
Run Code Online (Sandbox Code Playgroud)
-(void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
switch(gesture.state){
case UIGestureRecognizerStateBegan:
NSLog(@"State Began");
break;
case UIGestureRecognizerStateChanged:
NSLog(@"State changed");
break;
case UIGestureRecognizerStateEnded:
NSLog(@"State End");
break;
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
86344 次 |
最近记录: |