1

您好,我想模拟长按按钮?我怎样才能做到这一点?我认为需要一个计时器。你能帮助我吗?我明白UILongPressGestureRecognizer了,但我怎样才能使用这种类型?

这是代码,它不识别长按

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.button addGestureRecognizer:longPress];
[longPress release];

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
         NSLog(@"Long Press");
    }
}
4

1 回答 1

2

为了使用UILongPressGestureRecognizer你必须设置minimumPressDuration属性。这指定在触发手势识别器之前等待多长时间。例如

UILongPressGestureRecognizer *longPress = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)] autorelease];
longPress.minimumPressDuration = 2.0f;
[self.button addGestureRecognizer:longPress];

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
         NSLog(@"Long Press");
    }
}
于 2011-11-01T13:11:59.900 回答