2011-05-15 87 views

回答

2

你可以不喜欢这样。

在您的.h文件:

NSTimer *touchesHoldTimer; 

和:

@property (nonatomic, retain) NSTimer *touchesHoldTimer; 

- (void)touchesHoldCheckTime; 

记住合成和释放touchesHoldTimer

在您.m文件:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSSet *countTouches = [event allTouches]; 

    NSLog(@"touchesBegan"); 

    if ([countTouches count] == 1) { // Not multitouch 
     NSLog(@"Starting timer.."); 

     touchesHoldTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(touchesHoldCheckTime) userInfo:nil repeats:NO]; 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"touchesEnded"); 

    if (touchesHoldTimer != nil) { 
     [touchesHoldTimer invalidate]; 
     touchesHoldTimer = nil; 
    } 
} 

- (void)touchesHoldCheckTime { 
    NSLog(@"You have hold me down for 3 sec."); 

    [touchesHoldTimer invalidate]; 
    touchesHoldTimer = nil; 
} 
2

你可能想看看如果UILongPressGestureRecognizer适合您的应用程序接口。上面的苹果文档有一个示例代码链接。 UIView只提供了四种重写方法。您可以使用手势识别器进行更多操作。 Here's其他手势识别器的示例代码。希望这可以帮助。