2010-11-27 65 views
0

好的,所以我希望有人可以对此有所了解。事情是我有一个应用程序,用户可以在其中与大量图标进行交互,这些图标可以在屏幕上拖动。点击一次在打击垫打开它并点击并保持提出一个警报视图,询问您是否要删除它。我正在使用一个计时器来检查用户是否正在点按并在touchesMoved和touchesEnded事件中使计时器无效。问题是,如果你在pad上快速点击,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 
     } 

回答

0

您可以尝试使用UIGestureRecognizer s代替。

你将不得不建立手势识别您的所有垫

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上阅读。它可以为您节省大量的工作。

+0

谢谢,我会试试看。 – Glitch 2010-11-28 11:10:50