2010-11-27 50 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