UIAlertView警报在长按手势识别器内重复三次
我创建了一个应用程序。通过开发它,我用长按钮显示警报。UIAlertView警报在长按手势识别器内重复三次
这是我的代码:
- (IBAction)longPressDetected1:(UIGestureRecognizer *)sender {
// label1.text = @"Select Iran to observe its historical data projections ";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
message:@"Press the blue button (+) to select your region "
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
问:当我想通过cancelIbutton取消UIAlertView中,我必须按下此按钮三次,取消了UIAlertView中。这意味着UIAlterview只能通过一次按下才能取消。 你能帮我吗?
试试这个
- (IBAction)longPressDetected1:(UIGestureRecognizer *)sender {
// label1.text = @"Select Iran to observe its historical data projections ";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
message:@"Press the blue button (+) to select your region "
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
alert.tag =10;
[alert show];
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
switch (alertView.tag)
{
case 10:
if (buttonIndex==0)
{
}
break;
}
}
这是如何解决显示多个警报视图或需要点击取消三次的问题? – rmaddy 2013-03-09 04:37:06
我想你在视图控制器中使用多于一个的alertview – Ravindhiran 2013-03-09 04:42:03
的问题是,您都出现了不止一个警报视图。你的长按处理程序将被调用不同的状态。
从文档的UILongPressGestureRecognizer
:
长按手势是连续的。当在指定的时间段(minimumPressDuration)按下允许的手指的数量(numberOfTouchesRequired)并且触摸不超过允许的移动范围(allowableMovement)时,手势开始(UIGestureRecognizerStateBegan)。手指移动时,手势识别器转换到“更改”状态,并且当任何手指抬起时手势识别器结束(UIGestureRecognizerStateEnded)。
因此,您最终显示“开始”状态,然后是“已更改”状态,并再次显示“已结束”状态的警报。如果你移动你的手指,你会得到一个“改变”状态的流,你最终也会为他们每个人显示一个警报。
您需要确定何时实际希望显示警报。你想让它出现在第一个“改变”的状态,或者当用户举起他们的手指,你得到“结束”的状态。
你的代码需要是这样的:
- (IBAction)longPressDetected1:(UIGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
// label1.text = @"Select Iran to observe its historical data projections ";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
message:@"Press the blue button (+) to select your region "
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
}
当用户提起他们的手指这将显示警报。如果您希望在识别长按时立即显示警报,请将UIGestureRecognizerStateEnded
更改为UIGestureRecognizerStateChanged
。但请记住,由于长按可以生成多个“更改”状态,因此您仍然可以获得倍数。在这种情况下,您需要添加一个额外的检查,以便只显示警报,如果还没有。
其实,这里有一个简单的方法来支持的“更改”状态的单个警报:
- (IBAction)longPressDetected1:(UIGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateChanged) {
sender.enabled = NO; // Prevent any more state updates so you only get this one
sender.enabled = YES; // reenable the gesture recognizer for the next long press
// label1.text = @"Select Iran to observe its historical data projections ";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
message:@"Press the blue button (+) to select your region "
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
}
好的答案,当你希望在长按被识别时出现警报,你需要与UIGestureRecognizerStateBegan进行比较,而不是......改变。 – fishinear 2013-11-27 18:03:27
这是一个老问题,但我也有类似的问题。
任何UILongPressGestureRecognizer会产生状态的至少4点的变化:
- 改变 - (即变更为开始)
- 开始 - (即启动)
- 改变 - (即变更为结束)
- 已结束 - (即结束)
所以要处理好,你需要选择哪些条件将激活一个动作。
对于UIButton来说,最简单的对应于'touch up inside'的方法是检测'已结束'状态。
下面是一个例子,其中“长按”是UILongPressGestureRecognizer
- (void)longPressedLastImage:(UILongPressGestureRecognizer*) longPress {
if (longPress.state == UIGestureRecognizerStateEnded) {
// do what you would in response to an equivalent button press
}
}
的情况下这可以让你把连续按动更像是基本的UIButton的行动。
'简单比复杂'更好 - T彼得斯 - Python的禅宗
这与PHP有什么关系? – Jodes 2013-03-09 03:56:20
对不起,这是ipad应用程序 – hooman 2013-03-09 04:13:06