2011-01-07 38 views

回答

1

如你所知,你應該使用的touchesBegan,移動,結束,並取消功能前3.2的iOS。 我認爲只實現touchesMoved是不好的,因爲如果用戶按下並且一直移動直到釋放,那麼touchesMoved將不會被調用。

相反,我使用NSTimer來實現長按觸摸事件。 這可能不是一個最佳解決方案,但它對我的應用程序運行良好。 以下是一段代碼。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    isAvailable = NO; 
    timer = [NSTimer scheduledTimerWithTimeInterval:DURATION target:self selector:@selector(didPassTime:) userInfo:nil repeats:NO]; 
} 

- (void)didPassTime:(id)sender{ 
    isAvailable = YES; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    if(isAvailable == YES){ 
     // still pressing after 0.5 seconds 
    } 
    else{ 
     // still pressing before 0.5 seconds 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    if(isAvailable == YES){ 
     // releasing a finger after 0.5 seconds 
    } 
    else { 
     // releasing a finger before 0.5 seconds 
      [timer invalidate]; 
      timer = nil; 
    } 



} 
相關問題