2010-11-27 4 views
0

좋아, 그럼 누군가가이 문제에 관해 밝힐 수 있기를 바랍니다. 것은 사용자가 화면에서 드래그 할 수있는 큰 아이콘과 같은 종류의 "패드"와 상호 작용할 수있는 앱을 가지고 있다는 점입니다. 패드를 한 번 누르면 탭이 열리고 탭을 길게 누르면 삭제할지 묻는 경고보기가 나타납니다. 타이머를 사용하여 사용자가 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 
     } 

답변

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에서 읽으십시오. 그것은 당신에게 많은 돈을 절약 할 수 있습니다.

+0

감사합니다. 나는 그것을 시도 할 것입니다. – Glitch

+0

좋아요, 그래서 UIGestureRecognizers를 추가하려고 시도했는데 한 가지 예외가 있습니다. 즉, 패드를 만질 때 약간 어둡게 보이기를 원합니다. 문제는 지금 UILongPressGestureRecognizer 및 UIPanGestureRecognizer에서 작동하지만 UITapGestureRecognizer에서는 작동하지 않을 수 있다는 것입니다. 사실, 내가 원하는 건 패드를 만질 때마다 당신이하는 일에 상관없이 패드가 어두워지는 것입니다. 제 문제를 일으키는 원인이되는 것인데 touchesBegan을 사용하지 않고 가능합니다. – Glitch

관련 문제