快速点击不会触发touchesEnded

时间:2010-11-27 09:39:44

标签: iphone cocoa-touch

好的,所以我希望有人可以对此有所了解。问题是我有一个应用程序,用户可以与许多“垫”交互,这些“垫”有点像大图标,可以在屏幕上拖动。在打击垫上点击一次打开它,点击并按住将显示一个警报视图,询问您是否要删除它。我正在使用计时器来检查用户是否在touchesMoved和touchesEnded事件中点击并保持和使计时器无效。问题是,如果你在一个打击垫上快速点击,touchesEnded事件永远不会被触发,因此会出现删除对话框,这会让用户感到有些困惑。对此为何的任何想法?

- (void) startTouchTimer:(float)delay 
{
    self.touchTimer = [NSTimer scheduledTimerWithTimeInterval:delay target:self selector:@selector(touchHeld:) userInfo:nil repeats:NO];
}


- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches] anyObject];
    if (touches.count == 1) 
    {
        for(WishPad *pad in self.view.subviews) 
        {
            if ([touch view] == pad)
            {
                //Animate Selection
                [UIView animateWithDuration:0.03
                                  delay:0 
                                options:UIViewAnimationOptionBeginFromCurrentState 
                             animations:^{
                                 pad.highlight.alpha = 0.5;
                             } 
                             completion:NULL];

                self.touchedPad = pad.padName;
                [self startTouchTimer:0.40];
                [self.view bringSubviewToFront:pad];
             }
         }
    }
}


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [self.touchTimer invalidate];
    UITouch *touch = [[event allTouches] anyObject];

    if (touches.count == 1) 
    {
        for(WishPad *pad in self.view.subviews) 
        {
            if ([touch view] == pad)
            {
                CGPoint location = [touch locationInView:self.view];
                pad.center =location;       
            }
        }
    }
}


- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event 
{
    [self.touchTimer invalidate];
    UITouch *touch = [[event allTouches] anyObject];

    if (touches.count == 1) 
    {
        for(WishPad *pad in self.view.subviews) 
        {
            if ([touch view] == pad)
            {
                //Animate deselection
                [UIView animateWithDuration:0.03
                                  delay:0 
                                options:UIViewAnimationOptionBeginFromCurrentState 
                             animations:^{
                                 pad.highlight.alpha = 0;
                             } 
                             completion:NULL];

                CGPoint location = pad.frame.origin; 
                NSString *position = NSStringFromCGPoint(location);
                iWishAppDelegate *delegate = (iWishAppDelegate *)[[UIApplication sharedApplication] delegate];

                for (Wish in [delegate.wishes objectForKey:@"<none>"]) 
                {
                   if ([position isEqual:Wish.position] && [[(WishPad*)[touch view] padName] isEqualToString:Wish.name]) 
                    {
                        self.goToAddWish = [[AddWish alloc] initWithNibName:@"AddWish" bundle:nil];
                        self.goToAddWish.editWish=YES;
                        self.goToAddWish.hidesBottomBarWhenPushed=YES;
                        [self.navigationController pushViewController:self.goToAddWish animated:YES];
                        [self.goToAddWish editDetailText:Wish];
                        [self.goToAddWish release];
                    }

                    else if ([pad.padName isEqual:Wish.name]) 
                    {
                        Wish.position = position;
                        [delegate save];
                    }
                }
            }
        }
    }
}


- (void) touchHeld:(NSTimer*)timer 
{
    [self.touchTimer invalidate];
    NSString *wishToDel = [NSString stringWithFormat:@"Delete %@?", self.touchedPad];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:wishToDel message:@"Do you really want to delete this wish?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil];
    [alertView show];
    [alertView release];
}


- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{

    if (buttonIndex == [alertView cancelButtonIndex]) 
    {
            //Lots of deleting from plist and deleting image stuff happening here
        }

1 个答案:

答案 0 :(得分:0)

您可以尝试使用UIGestureRecognizer

您必须为所有打击垫设置手势识别器

for(WishPad *pad in self.view.subviews) 
{
  UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(padTapped:)];
  [pad addGestureRecognizer:tapRecognizer];
  [tapRecognizer release];

  UILongPressGestureRecognizer *pressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(padPressed:)];
  [pad addGestureRecognizer:pressRecognizer];
  [pressRecognizer release];

}

你可以处理水龙头并按下

- (void)padTapped:(UITapGestureRecognizer *)recognizer {
  WishPad *pad = (WishPad *)recognizer.view;
  // handle tap for pad
}

- (void)padPressed:(UILongPressGestureRecognizer *)recognizer {
  WishPad *pad = (WishPad *)recognizer.view;
  // handle long press for pad
}

您可以使用minimumPressDuration控制印刷机应被识别为印刷机所需的时间。但请阅读UIGestureRecognizer。它可以为您节省大量的工作。